1

In my project I want to replace some strings with php variable and interations.

I have {Title} and this title I successfully replace with:

str_replace("{Title}", $this->title, $c); 

This works but I have question how can I replace this content:

Template content:

<% loop Users %>
    <p>$Title</p>
<% end_loop %>

Replace with this:

foreach($users as $var)
{
   <p>$var->Title</p>
}
7
  • 1
    Is it in twig ? Commented Jul 26, 2017 at 11:13
  • @Superdrac just about to comment the same - straight up php and twig are very different Commented Jul 26, 2017 at 11:14
  • no its normal php file. I have variables stored in db Commented Jul 26, 2017 at 11:14
  • Why reinvent the wheel? Why not use template engines? Commented Jul 26, 2017 at 11:16
  • 2
    And what? Silverstripe has no template engine? Commented Jul 26, 2017 at 11:19

2 Answers 2

0

Since PHP is kind of a template language itself you could do the following:

<?php foreach($users as $user): ?>
   <p><?= $user->Title ?></p>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

7 Comments

You cant store php in database!
Who told you that?
Please give me example how you store foreach statements in db... Please
Wow, this turns out to be an emotional thread! A downvote too! I actually agree with @Lars, that you can almost directly write what you want with the plain PHP sample given above. Maybe this answer does not directly solve the OPs question but it certainly contains "food for thought"!
I'd assume that you store the collection in a database (here: $users), not its iteration
|
0

Another post on stackoverflow: Search and replace multiple values with multiple/different values in PHP5?

I think you need to some thing which can be done like following:

$string = 'a ar b br c cr';
$result = str_replace(
  array('a', 'ar'), 
  array('b', 'br'), 
  $string
);

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.