Jump to content

PHP Placeholders


JKG

Recommended Posts

hello,

 

I am working on a site and have hit a bit of brick wall.

 

I use a little custom CMS i have built up and for this project i need to have a lot of different menus which i need to call from the CMS, which runs from a database.

 

Basically, at the minute i include them from the page, but would like it to be controlled through the CMS via place holders eg: {{rightHandMenu}}

 

Is there something that i can do so when the data is pulled from the db i use maybe a find a replace, that can sniff out that phrase, then parse with php to be eg: include 'blocks/rightHandMenu.php';

 

Im on a shared server and dont want to rewrite the whole site so i presume smarty is out of the window...?

 

Sorry its a bit wordy. :)

Thanks for reading!!

Link to comment
Share on other sites

Or str_replace(), if you are replacing fixed strings with other fixed strings.  You would most likely read in all the replacement values and use str_replace() on each one if using that approach.

 

preg_replace() has more advanced features, such as being able to call a function for each replacement, with the 'e' modifier.  That would allow you to include a different file depending on which tag was found, for example.

Link to comment
Share on other sites

thanks alot guys.

 

could you possibly please give me some idea of how to implement?

 

obviously it would be something along these lines:

 

$placeholders = array('{{rightHandMenu}}', '{{leftBox}}');
$vals = array('include1', 'include2');
$str = str_replace($placeholders, $vals, $body);

 

but how do i do the includes? i cant store them in a variable, and putting them straight in the array comes back with an error.

 

i am unfamiliar with preg_replace, but am going to research today.

 

Thanks!

Link to comment
Share on other sites

ah sorted it!

 

thanks for you help guys for any one else who wants to know:

 

the above does work with include strings, must have had a syntax error in it before.

 

thanks for pointing me in the right direction. :)

Link to comment
Share on other sites

oh, maybe not.

 

i think my page is performing the include within the array.

 

<? 
$placeholders = array('{{leftHandMenu}}', '{{leftBox}}');
$includes = array(include $root_dir . $html_dir . $includes_dir . 'blocks/leftHandMenu.php', 'include2');
echo str_replace($placeholders, $includes, stripslashes($body)); ?>

 

so when it reads the include statement, it actually parses it and performs the include, then when it reads the str_replace it just outputs 1.

 

any thoughts?

thanks. Joe.

Link to comment
Share on other sites

sorted. :)

but it isnt pretty!!

 

ill just dump the code here for anyone who wants to know:

 

$placeholders = array('{{leftHandMenu}}', '{{leftBox}}'); //what you want to find
$includes = array(getLefthandMenu($body), 'include2'); //what you want to replace with, respectively
echo str_replace($placeholders, $includes, stripslashes($body)); //perform find and replace

 

functions.php

function getLeftHandMenu($body) {
if(strpos($body, '{leftHandMenu}}')){ //had to miss out the first '{' cos otherwise strpos returns 0, which seems false
$leftHandMenuFile = '/fullpath/includes/blocks/leftHandMenu.php'; //full path to file you want to include
$leftHandMenuFileOpen = fopen($leftHandMenuFile, 'r'); //open with read perms
$leftHandMenu = fread($leftHandMenuFileOpen, filesize($leftHandMenuFile)); //read file
$evalLefthandMenu = eval('?>'.$leftHandMenu); //if file has php, use this to parse the php
fclose($leftHandMenuFileOpen); //good practise to close file after use
echo $evalLefthandMenu; //return the findings!
}
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.