We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

LG Replace - A different kind of find and replace *NEW*

Development and Programming

Leevi Graham's avatar
Leevi Graham
1,143 posts
17 years ago
Leevi Graham's avatar Leevi Graham

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:

  1. 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.

  2. 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}.

  3. 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

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

I can tell you at least 50 times I could have used something like this. I’ll give it a try. Thanks.

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
17 years ago
Leevi Graham's avatar Leevi Graham
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

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
17 years ago
Leevi Graham's avatar Leevi Graham

Dam I new I had seen something like this before!

http://ellislab.com/forums/viewthread/37240/

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

Leevi - I believe you have the wrong plugin inside the Zip. I keep extracting “Latest EE”, and I could use this plugin stat!

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
17 years ago
Leevi Graham's avatar Leevi Graham

Fixed!

       
Meirion's avatar
Meirion
127 posts
17 years ago
Meirion's avatar Meirion

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.

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
17 years ago
Leevi Graham's avatar Leevi Graham

Yeah this is possible…. I have done it on http://bitsandpixels.com.au/about/ where it says: “The next event is on Saturday 21st June”. The date is replace from outside of the loop.

       
Meirion's avatar
Meirion
127 posts
17 years ago
Meirion's avatar Meirion

Flippin’ brilliant

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

Whoa. I can’t wait to get a few minutes to experiment with this. I’ve downloaded but haven’t had a chance yet - but that ability to insert images in there like Meirion mentions…I think I just had an epiphany about how to use this on my current site build. Cool!

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

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!

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
17 years ago
Leevi Graham's avatar Leevi Graham

– deleted by author –

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

Nice. I just used it to make a nice little “Read More” link on a single miscellaneous entry, without needing an excerpt plugin. This thing rules!

       
Casey Reid's avatar
Casey Reid
82 posts
16 years ago
Casey Reid's avatar Casey Reid

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 😊

       
Danny T.'s avatar
Danny T.
426 posts
16 years ago
Danny T.'s avatar Danny T.

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!

       
1 2 3 Last

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.