1

I use PHP Smarty and now I want to use replace: on a more correct way.

I want to remove the entire <div class="news-text"></div> and its inner content.

How can I achieve that?

My code currently looks like this, but it is nasty because it only hide the content:

{$announcement.text|replace:'<div class="news-text"> ':'<div style="display:none;" class="news-text">'}
3
  • Why not just {$announcement.text|replace:'<div class="news-text"> ':''}? That said, this should probably be handled outside the view, and handled via a proper DOM manipulation. Commented Sep 11, 2016 at 15:07
  • @ceejayoz Thanks! Well because than the inner conent of the <div class="news-text"> still is displayed/loaded. And I need a way to remove the entire <div class="news-text"> with the inner content. Commented Sep 11, 2016 at 15:22
  • Then you really need DOM manipulation. php.net/dom Commented Sep 11, 2016 at 15:35

1 Answer 1

1

Smarty cannot do that in a reliable way. You can try to create a custom plugin to manipulate html or better, modifiy the contents of the variable before sending it to Smarty.

Your current solution is also unnecessary and may be prone to errors. You can do it with simple css, no replace required; i.e.:

html:

<div class="no_news_text">
{$announcement.text}
</div>

css:

.no_news_text .news-text{display:none !important;}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.