1

Here is the scenario.

I have text in the DB that looks like this:

blah blah blah blah blah {$name} {$index}

As you may see it contains two Smarty variables {$name} and {$index}

Now I need to assign the values to the variables with Smarty. For those who do not know Smarty, values can be easily assigned this way:

$smarty->assign('name_of_the_variable', $variable);

The problem is that this text is coming from the DB, and I do not know which variables will be in the text, so I am trying to abstract the code doing the following:

    function getPageContent($page) {

        $smarty = new Smarty(); // initializing Smarty

    //selecting the content and saving it into the $content variable
        $q='SELECT * FROM pages_blocks WHERE Page="'.$page.'"';
         $r=mysql_query($q) or die(mysql_error());
             while($row = mysql_fetch_array($r)) {
              $content = $row['Content'];
               }

//defining variables
        $name = "NAME";
        $index = "INDEX";

    //getting all the variables inside brackets {}
               preg_match_all('/{(.+)}/U', $content, $matches);


    //abstracting the code assigning values to the variables found   
        foreach ($matches[1] as $match) {
         $foo = str_replace('$', '', $match); //removing the $ to give a name to smarty
          $smarty->assign(''.$foo.'', $match); //final assignation of name and its variable
         }
           $smarty->display('string:'.$content); //displaying the final content
        }

The problem is that the final content looks like:

blah blah blah blah blah $name $index

Instead of

blah blah blah blah blah NAME INDEX

Something is wrong. Smarty is printing the variables as they are instead of running them before as normal.

Please help me.

4
  • I tried also to eval($match) but with no luck at all. Commented Jan 31, 2012 at 0:53
  • 1
    don't use smarty. PHP is already a powerful template engine Commented Jan 31, 2012 at 0:55
  • Good advise. But it's not helping me at all at the moment. I am using Smarty and I need a solution using it... Commented Jan 31, 2012 at 0:56
  • I know that's why i just made a comment Commented Jan 31, 2012 at 0:56

2 Answers 2

3

Replace this:

$smarty->assign(''.$foo.'', $match);

With this:

$smarty->assign(''.$foo.'', ${$foo});
Sign up to request clarification or add additional context in comments.

5 Comments

Why? What was the problem? I am curious
eval isn't the right way to load a variable based off another variable name. ${...} is the way, where "..." is like a string with interpolation, so you can put other variables in there.
For example, consider the code: $a = '1'; $b = 'a'; echo ${$b}; Will echo "1".
ok but it's not working with arrays, variables like $lang['city'] does not work..can you help me?
For something like that, you'll have to divide the string into components, lang and city (use a regexp or however you want to do it). So say you have them separated into variables called $key (== 'lang') and $ind (== 'city'). Then you can do ${$key}[$ind].
0

You're using regexp {(.+)}, .+ will "eat" everything it sees, so it'll match: $name} {$index.

You should use greedy killer: ''/\\{(.+?)\\}/U''

1 Comment

@yes123 any particular reason? I found it as good practice mostly in combination with characters and numbers for case that they get special meaning in future, such as \d may be interpreted as DEL character or so.

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.