/    Sign up×
Community /Pin to ProfileBookmark

Requiring assistance

I have created various template engines in the past. However, good or not as they were, I found them fatally inefficent. Their manner used functions and thus required massive globals and eval()’s to be called at every instance, and I just thought there had to be a better way.

I want to write a function that will take the template into a string, search for variables in form $variablename and globalize them in some manner. This however, seems to be beyond my ability to find a clean way of doing such.

I would also like to create another function (if not the same) that would take the template into a string, find <if condition=”> and parse it as a normal if parameter.

If anyone can help me with this, it would be a great help as it is necessary for me to strive for efficency.

Thanks again ? ?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanSep 09.2004 — Well, I guess you could use a database or arrays to store variables and to globalise a value all you do is put the key word global before its name.


RyanJ
Copy linkTweet thisAlerts:
@HellspireauthorSep 09.2004 — Yes yes...

I need to know which regular expression function can be used with a template string to find variable names in the format $variablename

... is it preg_match_all, hmm? // I think it is.

Furthermore, what would the regular expression be for finding <if condition="blah"> and displaying its if or else relative to the if condition.

Sigh... its not that I dont know how, I just need to know the specifics of the higher end required on this project.

Also could this be used to globalize variables.


**REMOVED**


would doing that effectively initalize them as globals???

Thanks again,
Copy linkTweet thisAlerts:
@sciguyryanSep 09.2004 — [i]Originally posted by Hellspire [/i]

[B]Yes yes...



I need to know which regular expression function can be used with a template string to find variable names in the format $variablename

... is it preg_match_all, hmm? // I think it is.



Furthermore, what would the regular expression be for finding <if condition="blah"> and displaying its if or else relative to the if condition.



Sigh... its not that I dont know how, I just need to know the specifics of the higher end required on this project.



Also could this be used to globalize variables.



[code=php]
#$variables is an array with all variables from the template.
foreach($variables as $value){$globalstring .= $value.',';}
$globalstring = substr($globalstring,0,(strlen($globalstring)-1));
global $globalstring;
# Recall template, and output it.
[/code]


would doing that effectively initalize them as globals???

Thanks again, [/B][/QUOTE]


1)I'd suggest this one: [url=www.php.net/preg_match]preg_match()[/url]

2)Well, just use a [url=www.php.net/preg_match]preg_match()[/url] statement inside an if statement because it returns true if found and false if not found so, you can do it that way.

3)Yes, it will initialize them globaly.
Copy linkTweet thisAlerts:
@HellspireauthorSep 09.2004 — Thanks for the info, and yes, I know of the preg_match and preg_match_all functions. Note that preg_match fetches only one. :p

I think I have it, however, I am at school and wont be able to check on it till later, so if anyone can test it for me, much abiliged.

To set up a test, simply make a string in a function and put some variable names in it, declare the variables outside the function and then add the following code in the function.

**REMOVED**

If this works, anyone is free to use this with my thanks. But I dont depend on it working. Being in college during the day, does not grant me the ability to have the resources I do at home, or my server for testing.

thanks again. :p
Copy linkTweet thisAlerts:
@sciguyryanSep 09.2004 — No, you had an error in your code I've corrected it and it works fine:

[code=php]
<?php
$name = 'bill'; $age = '999'; $height = '6ft';
function gettemplate($name){
// name is only if we were using mysql but for the test just ignore it.
$string = "my name is $name, and my age is $age, but my height is $height";
$matches = preg_match_all("/$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*/i",$string);

foreach($matches as $arrayvar => $value){$globalstring .= $value.',';}
$globalstring = substr($globalstring,0,(strlen($globalstring)-1));
global $globalstring;
// i think this may cause a problem because the string var was initialized before the preg check.. I think... but i doubt
eval("$string = "$string";");
return $string;
}
?>
[/code]


I'm presuming the x7f in your regexp are supposed to be hexidecimal (Ned to be escaped).

RyanJ ?
Copy linkTweet thisAlerts:
@HellspireauthorSep 09.2004 — Thanks ryan for testing it for me. Yes i notice now the error, I assumed I used the special regexp s for white space etc etc... This is good, I will be implementing it later on.

BTW. the regular expression was from the php document and i just added the $ because the example was only for the type of character sets it could use. those last bits are extended characters in the ascii chart. (available somewhere on the php document.)

Waves, Runs away.
Copy linkTweet thisAlerts:
@HellspireauthorSep 09.2004 — I apologize for replying to myself, but i thought it necessary so that everyone understand somehing. The code was flawed in many respect, the testing that someone did for me, apparent was a lie. Well what can you expect *shrugs* I'm now home and I will have the working model later today :mad:

Furthermore... the 'error' in my code was not an error.... ugh! He apparently has his warning level set WAY TOO LOW.

Edit:

Here is the working version
[code=php]
$name = 'bill'; $age = '999'; $height = '6ft';
function ahab(){
$string = 'my name is $name, and my age is $age, but my height is $height';
preg_match_all('/$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*/',$string,$matches);
for($i=0;$i<count($matches[0]);$i++){
$newmatch = substr($matches[0][$i],1,strlen($matches[0][$i]));
global $$newmatch;
}
$string = "my name is $name, and my age is $age, but my height is $height";
echo $string;
}
ahab();
[/code]


  • * All old codes posted by me have been removed. Enjoy everyone.

    Its ironic I have to do the code myself = ?
  • ×

    Success!

    Help @Hellspire spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...