Jump to content

parse php through html...


Dragen

Recommended Posts

I'm not sure if this can be done, but I think I might have seen it before.. can't remember.

 

I'm trying to get my php pages to parse php within HTML, instead of php brackets.

Basically instead of having:

lets echo a variable/constant: <?php echo $somevariable . ' or ' . SOMECONSTANT; ?>

 

I want to use somhing like:

lets echo a variable/constant: {$somevariable} or {SOMECONSTANT}

Is this at all possible?

 

I'm not wanting to do anything more difficult than echoing simple variables or constants..

Link to comment
https://forums.phpfreaks.com/topic/62357-parse-php-through-html/
Share on other sites

<?php
$myname = "bob";
<html>
Hello World My name is: <?php echo $myname;?><br/>

 

Thanks, I know that would work, but I'm trying to find a way of working it without the need of using <?php echo; ?>

 

Maybe there's some way of setting up a page to parse all text between {} as php, instead of html.

It's for a script I'm working on that is going to be used by different people, who probably wont know php, but may have basic knowledge of html, so instead of having to put in lots of echoes every time they want to output a variable I wanted it to be as simple as entering the variable name..

If you are writing a script for someone who doesn't know scripting you should look into using a database to store data and then recall it.  Also look into smarties or phpBB it lets you create psedo tags that interpret however you want, but not in the raw script, but in a text box sorta like saying colon left bracket and having an emodicon show up.

You could do something like this:

 

$html = '<h1>{$header}</h1><a href="{$link}">{$name}</a>';

$tpl = array('header' => 'This is a header!', 'link' => 'http://google.com/', 'name' => 'Google');

foreach($tpl as $k => $v) {
$html = str_replace('{$' . $k . '}', $v);
}

echo $html;

//it would echo:
//<h1>This is a header</h1><a href="http://google.com/">Google</a>

Also look into smarties or phpBB it lets you create psedo tags that interpret however you want, but not in the raw script, but in a text box sorta like saying colon left bracket and having an emodicon show up.

ah cooldude! I think you've got it.. must've been phpbb where I'd seen it before..

 

I'm trying to create a templating system, I guess pseudo tags or similar is what I need to implement...

could you give me any advice on how to do something like that? or any links would be great!

 

Thanks

okay, I see how that works if all of the text is stored in the database, such as forum posts or news articles etc.. using a simple preg_replace or similar..

But what I'm looking for is if they enter the pseudo text in a template file.

 

for example.. (I've just searched through my phpbb download) in index_body.tpl for the default template it's got this:

<td><span class="gensmall">{L_NEW_POSTS}</span></td>

and the {L_NEW_POSTS} is parsed as php instead of html.

 

 

EDIT: just had a closer look at phpbb and I think it's using classes.. which is something I've never understood before.

Does anyone know any good sites about classes? I've tried searching google and php.net, but couldn't find much..

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.