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
https://forums.phpfreaks.com/topic/234296-php-placeholders/
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
https://forums.phpfreaks.com/topic/234296-php-placeholders/#findComment-1204267
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
https://forums.phpfreaks.com/topic/234296-php-placeholders/#findComment-1204383
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
https://forums.phpfreaks.com/topic/234296-php-placeholders/#findComment-1204394
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
https://forums.phpfreaks.com/topic/234296-php-placeholders/#findComment-1204410
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.