Page 1 of 1
how to make a placeholder
Posted: Wed May 22, 2013 6:00 pm
by nite4000
got a fast issue
I need to take a variable like $var['sitename'];
but I want to be able to display that value by just entering [sitename] in the brackets.
Not sure anyone can help me with this or not
Re: how to make a placeholder
Posted: Wed May 22, 2013 6:05 pm
by mecha_godzilla
Hi,
Are you trying to populate a page template i.e.
1. Find every instance where a line contains [????]
2. Substitute it for the contents of $var[????]
3. Echo out the page template to the browser
HTH,
M_G
Re: how to make a placeholder
Posted: Wed May 22, 2013 6:10 pm
by nite4000
actually that may be backwards. someone did it for me a long time ago where when I put [sitename] on the page it got the value of the $var['site_name'] from the database
Re: how to make a placeholder
Posted: Wed May 22, 2013 6:31 pm
by mecha_godzilla
I think the method will depend on how you access the information in your database, so I'm not really sure what to suggest - does something like the following code look useful:
Code: Select all
$placeholder_value = '1234';
$template_text = "For example, you could use a [placeholder_value]";
echo preg_replace('/\[(\w+)\]/e', "\$\\1", $template_text);
// output: For example, you could use a 1234
Where you have the replacement string, instead of substituting the placeholder for a variable of the same name, you could hard-code the name of the database object in there and match against a property of that object.
M_G
Re: how to make a placeholder
Posted: Wed May 22, 2013 6:40 pm
by nite4000
that looks similar I am actually searching my 1TB drive trying to find where the code was if I still have it but that does look something like it
the variable is am using is $a_settings['sitename'];
in a page I have it where the text of the page is in a field in the database for easy editing for the user and in that text I want to just use [sitename] so they don't have to ask for the php code
I will try this and see how it works
Re: how to make a placeholder
Posted: Wed May 22, 2013 6:57 pm
by mecha_godzilla
If $a_settings is just an array of values then you could use the following code:
Code: Select all
// For testing purposes
$a_settings = array();
$a_settings['sitename'] = 'My Site';
$template_text = "Welcome to [sitename]";
echo preg_replace('/\[(\w+)\]/e', "\$a_settings['\\1']", $template_text);
// output: Welcome to My Site
M_G