Dependencies with templates

This isn’t meant to be an insult at all, but I think if you would learn how to properly program in PHP first, that would probably give you a whole lot more of what you want and faster in the future. And, when you get stuck, we can actually help you too.

Scott

4 Likes

^^^ Wow! Imagine. I just came on line now to ask a question only to see your innocuous and polite comment on my thread with many likes to booth. Amazing, really. I simply don’t understand why everyone here seems to be beefing me as though I’ve previously wronged you all or viciously murdered one of your relatives. Someone makes a snide remark in reference to me and you all fall head over heels in a bid to be the first to upvote that comment (The usual culprit and most patriotic of them all being @colshrapnel). I don’t know what this is about though but its quite distasteful and discouraging.

If there is a levy each new member must pay before being inducted and not treated like an outcast/vagabond, you all should fill me in so I can stop defaulting at once and hastily remit my dues and join the brotherhood (and probably devour other new unsuspecting members too *winks)

Jesus! In as much as my (lack of) input here would not influence the progress of your esteemed site/brotherhood board, I think I’m done here. I’ll stage a comeback when I can deal with such level of insidious and malicious wood-pecking. Au revoir!

So much random talk I cannot even get my head around.
On the other side try to learn something about template engines before going in full defense mode.

Twig is a good and powerful template engine that you can use in your system if you want.

I think it’s normal.

There are strong personalities who just can’t stand the common ways and whose ideas nobody understands. Occasionally they end up founding a new religion or a prgramming language.

They only need to learn to bear with their frustration and keep their own way. After all, all of the questions asked by the OP so far, he answered humself. Thus, community can be of little help anyway.

Besides, I have a feeling that the op may choose writing essays over programs. The last comment was really impressive.

This is an interesting thread, but I’m not too sure where it’s heading, or even if it should try. Whilst I understand it’s not always easy to extrapolate from incomplete data, we all started somewhere, and it wasn’t the same place as the person now stood next to us. Maybe a little humility or a breather maybe called for. What say you?

7 Likes

How do you politely tell someone they should go learn more about something? I mean, that statement could easily be rephrased as, “You are ignorant”, right? Whereas, that wasn’t my intention at all.

I am someone who believes in the saying…

Give a man a fish, he can eat for a day. Teach a man how to fish, and he can eat for a lifetime.

The only problem is, I (and the other members of this forum) can’t teach someone PHP basics on a forum. So I suggested to @nmeri17 to go learn more about PHP, because he is obviously missing a ton of basics and he even said he just started learning a day earlier. So, was I actually wrong to do so? Maybe. Though I was genuinely trying to help him.

Scott

But being told to go away is almost never a good approach to take. The biggest problem with that approach is how does a new person know where to get the best advice? Not everyone finds php.net or W3C or other authoritative sites easy to follow and digest. They could easily end up following the instructions of a site like W3Schools, which while technically accurate, aren’t the most up to date standards/concepts.

Now, I’m not telling you to hold his hand and walk him through rewriting his site, but perhaps even if you provide links to specific topics where you think the poster is lacking the most, that’s always a better approach.

1 Like

FOR CLARIFICATION PURPOSES:

Bro? My request was simple–maybe what I really need is to go back and retake my English classes. I said I have got a template, a php file that needs one primary variable which guides it on how to mould that page. I don’t know how better to illustrate or give allusions, unfortunately. But apparently, no one understood nor saw it the way it was in my mind’s eye and instead of moving along, they stuck back to make haughty comments. At the end of the day, what I did was use constants like I said earlier. The code looks something like

$page_init = file_force_contents($_SERVER["DOCUMENT_ROOT"]."/$new_path/index.php", "<?php define('pageOwner','" .$row['username']. "',true); ?>\n");
$user_doc = file_put_contents($_SERVER["DOCUMENT_ROOT"]."/$new_path/php", template.php, FILE_APPEND);
/* file_force_contents is a custom recursive function that checks if the directory doesn't exist and creates it since file_put_contents returns false on absence of specified path */

Then in the template.php file, I have something like

$conn = new mysqli("ost", "root",  "57", "ral_db");
if ($a = $conn->query("SELECT * FROM user_info WHERE username='". pageOwner. "'")):
while ($owner = $a->fetch_assoc()):
	<div> <?php echo $owner['bio']; ?> </div>
	<img src='<?php echo $owner['picture']; ?>' alt=''>
// etc etc

That’s how I intend grabbing needed variables from the mother script and using them here. By “Dependencies with templates”, I’m trying to describe the child script depending on the mother for certain variables. The mother script already contains markup which, if the page was simply included, would cause all that markup to spill into the innocent new page.

I sincerely hope the non-malicious ones and probably, people from the future trying the same thing will be clear by this clarification I’m making for them alone.

The subject of writing templates is too fascinating to pass on to some other ‘guru’ who did god-knows-what to make it work.

^^^ This weighs in much more than all your comments combined, no offence though. You showered me with compliments over and over. Sarcasm aside, I actually write prose as a hobby and write code as pro work. For a daunting person like you to admit I am good at something, my goodness must be glaring then. Sadly, I already knew though. :frowning:

I just might take your strong advise someday and pioneer a program of my own strictly for nutcases like myself while you and your cohorts hop from blog to blog, fervently condemning the language (like you once condemned its founder) and chivalrously imploring other programmers to avoid it like the bubonic plague.

Erm, on a side note, we could be friends if you want. I could become your English teacher in exchange of you inducting me into the brotherhood here. What say thee bro??

What you said was grossly malicious because you failed to point out what basics it is you’re referring to.

Dude, you really believed me when I said I started learning PHP a day before? What, is sarcasm prohibited in your country of origin? If you stop following a person’s code at some point, the logical thing to do is ask them what it is they had in mind at that point, not imply they are losers.

See, I’ve noticed a feature (or should I say trojan) ubiquitous amongst every supposed senior programmer I have come across in my little life: they ALWAYS treat us younger guys as though we will never get to the point they are at now. Please, I’ve given you the heads up. Try and delete it.

PS: Please kindly indicate the basics you were referring to earlier on.

Sarcasm is hard to read. Since you were kidding, how did you learn PHP and how long have you been programming in PHP? How long have you been programming? What other languages have you learned?

By the basics, I mean, the basics. Like how PHP should work in general. For instance, @oddz said it would be the wrong way to go about solving your problem by creating a file per request. The fact you want to still do this shows you are missing some very basic knowledge.

Agreed. And that wasn’t my intention either. But, it is like the student in a school, who fails in a class. That student would have to leave that class or go to summer school to “retake” the class to catch up. This is similar.

Maybe I shouldn’t have been the judge. For sure I am not trying to be judgemental. I am simply trying to help @nmeri17.

Scott

Dude, it’s a content management system; every request from that tab would create a new file anyway. Whenever the administrator goes to that particular tab, he already knows he’s going to create a new file. It’s not a public interface.

You failed to provide an alternative.

You failed to enumerate even two basic knowledge out of the vast body of knowledge PHP encompasses which I’m missing. Bro? Please go to sleep.

Nowhere did I purposely insult you. And I am not your Bro.

I’ll leave this conversation, as obviously, you know more than I do about what you think you know.

Scott

1 Like

Fine. I am a dunce and I have no idea what I’m doing. Can you kindly enumerate specific basics I’m missing and reply the thread stating how to solve the problem in the OP, my good Sir?

When I suggested a breather a little earlier, I was hoping that some might have taken that as a hint to calm things down a little. Please confine the discussion to the topic at hand, rather than each other.

3 Likes

I’m in strong support. I think I have openly declared myself a dunce and agreed to writing meaningless lines of PHP. The other guy swatted off my “bro” and arm of friendship. Let’s hope he comes up with the expert solution to the issue at hand like I begged him to in my last post so we can all learn and I can elevate from my dense ignorance.

Ok. Here is your partial solution. The basics on a templating system in PHP.

https://paulund.co.uk/php-template-system

This is also a good read about a great template system called twig. It is probably one of the best in the industry.

http://twig.sensiolabs.org/doc/api.html

And this is how the API looks for includes, which is what I think you are looking for.

http://twig.sensiolabs.org/doc/tags/include.htm

And here is the way twig also handles inheritance, which might be of interest to you too.

http://twig.sensiolabs.org/doc/templates.html#template-inheritance

Scott

A very basic and functional example of what I believe your trying to achieve could be:

function template($path, $global=null) {
  //Extract any global set values
  if (!empty($global)) {
     extract($global);
     }

  ob_start();
  include $path;
  $contents = ob_get_contents();
  ob_end_clean();

  return $contents;
  }

$global_array = array('pageOwner' => 1,
                      'info' => array()); //etc.

echo template('/path/to/template/file', $global_array); 

The key used in the global array decide the variable name it get when parsed in the function, i.e. ‘pageOwner’ become $pageOwner etc.

If you want to make your own solution, the code above can refactored into OOP with getter/setters etc. to fit what you need.

In the event you just need a basic template engine, Twig would not be a bad choice.

^^^ Thank you all.

Even though this may be against the site’s rules but I’d like to PM with anyone willing to help me solve some issue concerning the last section of my site(Mods please edit off this part if it’s against the site’s rules). If anyone is chanced or would volunteer, kindly click my profile and send a message. Thank you once again.

If it was against the rules to PM then they’d have turned off that option on the forum so that members couldn’t PM one another through the forum.

People don’t need your email address to PM you, they just need to click on your forum name and then select to message you.