EEConf 2024 is around the corner! EEConf 2024
LG Replace is a different kind of text replacement plugin.
How many times have you wanted to add a dynamic date inside some user content? Or add some related links half way down an entry?
Now you can with LG Replace! Its simple to use heres the quick rundown:
Wrap your content in the {exp:lg_replace:haystack}{/exp:lg_replace:haystack} template tag and list the needles you would like to replace in the needles=”” attribute.
Add the “needles” inside the {exp:lg_replace:haystack}{/exp:lg_replace:haystack} tags. The needles must be in the following format {my_needle_id} and the needle_id must be listed as one of the needles in the needles=”” attribute of {exp:lg_replace:haystack}{/exp:lg_replace:haystack}.
Now that the “needles” are in our “haystack” we need to tell the plugin what to replace them with. This is done using the {exp:lg_replace:replacement}{/exp:lg_replace:replacement} tag. This tag must have an needle_id=”” attribute that links it to the needle inside of {exp:lg_replace:haystack}{/exp:lg_replace:haystack}.
That’s it. Checkout the demo below as a test.
{exp:lg_replace:haystack needles="test1|test2|test3|test4"}
{exp:lg_replace:replacement needle="test1"}
Replace {test1} with this content
{/exp:lg_replace:replacement}
{exp:lg_replace:replacement needle="test2"}
Replace {test2} with this content
{/exp:lg_replace:replacement}
{exp:lg_replace:replacement needle="test3"}
Replace {test3} with this content
{/exp:lg_replace:replacement}
{exp:lg_replace:replacement needle="test4"}
Replace {test4} with this content
{/exp:lg_replace:replacement}
Here is some content which will be replaced:
- {test1}
- {test2}
- {test3}
- {test4}
{/exp:lg_replace:haystack}
I also should mention that the content of {exp:lg_replace:replacement}{/exp:lg_replace:replacement} can include other EE tags which makes this plugin very flexible and powerful.
Try it out… give it a kick in the tyres and then I will move it over to my site with better documentation
I can tell you at least 50 times I could have used something like this. I’ll give it a try. Thanks.
Yeah it seemed so simple that you would expect someone has done it before… Maybe someone has :(
I needed it for a project and couldn’t find anything so I thought I would build it myself 😊
Hope you like it
Levi, could I use this to insert images within a weblog post? for example:
{exp:lg_replace:haystack needles="test1|test2|test3|test4"}
{exp:weblog:entries require_entry="yes"}
<h1>{title}</h2>
{body}
{/exp:weblog:entries}
{exp:lg_replace:replacement needle="img1"}
/images/{image1}
{/exp:lg_replace:replacement}
{exp:lg_replace:replacement needle="img2"}
/images/{image2}
{/exp:lg_replace:replacement}
{/exp:lg_replace:haystack}
{image1} and {image1} are fields in the weblog that have image filenames, and then within the text of each weblog entry I have {img1} and {img2} tags.
With one small needle variable “{googlemap}” I was able to drop in a whole map area with links and other stuff right into the middle of a huge block of user-controlled content. Not sure how I would have done that otherwise, other than suggesting moving the map to the bottom of the page or something. Totally awesome, man. Thanks, Leevi!
There seems to be an issue with this plugin when using it with the TEH textile extension on a custom field. Basically I wanted to insert a link to a PDF document in the middle of a block of text. I was inserting “{pdf}” (no quotes) into the block of text within the CP, then using LG Replace to replace “{pdf}” with the actual link to the PDF which is a custom field called {static_pdf}.
Here is my code:
{exp:weblog:entries weblog="site_pages" limit="1"}
{exp:lg_replace:haystack needles="pdf"}
{static_body}
{exp:lg_replace:replacement needle="pdf"}
{static_pdf}
{/exp:lg_replace:replacement}
{/exp:lg_replace:haystack}
{/exp:weblog:entries}
When {static_body} has the formatting set to Textile the replace doesn’t work, but changing the formatting to XHTML seems to work fine. Has anyone else been able to get this to work with Textile as the formatting option? Otherwise I’ll go a different route, maybe LG TinyMCE 😊
I’m trying to understand how you guys are using this - in particular, Masuga.. Let me get this straight.
I have a haystack, and I designate my needle terms.. These needle terms can be inside a weblog entry body like so {needle1} and I can actually replace needle1 with a weblog custom field wrapped in code? For example, an image link inside the field processed with Image Sizer?
When the weblog entry is viewed, {needle1} would be replaced with the content as mentioned above?
If it works as above, this would be SUPER handy for things like tutorials, etc. written by members. All I’d have to do is teach them the tags that correspond to a field, such as {image1} and they can apply the images where they want. I’d format the images to make sure they are blocks, and it’d work perfect.
Another great contribution - thanks Leevi!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.