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
how to make a placeholder
Moderator: General Moderators
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: how to make a placeholder
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
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
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
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: how to make a placeholder
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:
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
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 1234M_G
Re: how to make a placeholder
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
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
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: how to make a placeholder
If $a_settings is just an array of values then you could use the following code:
M_G
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