Jump to content

Trying to server different Smarty Templates on Single Page


jcarney1987
Go to solution Solved by maxxd,

Recommended Posts

So I'm learning OOP PHP and I'm working on Seperating all my logic from display. 

So given than, I have a page that displays all my customers, but on the same page if I choose to add a new customer, I played on using the same PHP file to server up a new smarty template, but this is where I'm not sure how to do this without making it look funky.  I started off by passing a Get Method such as

https://localhost.cxm/contact.php?addcustomer=1

but I wanted to know if there was a better way to clean it up instead of making the link like that.  Here is my code

	if ($logged_in == TRUE){
	$smarty->assign('firstname', ucfirst($_SESSION["useruid"]));
	$smarty->assign('customers',$customer);
	$smarty->display('template/glory/header.tpl');
	 
	if (!empty($_GET['addcustomer'])){
    $smarty->display('template/glory/addcustomer.tpl')
}
else {
    $smarty->display('template/glory/customer.tpl');
    }
}
	else {
    $smarty->display('template/glory/login.tpl');
	}
	?>
	

Link to comment
Share on other sites

It sounds like you're looking for a pretty URL? If so, this is typically set up as a rewrite rule. I haven't looked at Smarty in a long time, and a quick Google search didn't return anything it specifically does to enable pretty URLs, so it's probable you'll have to update your .htaccess file (assuming you're using Apache - I'm not sure where or how this happens if you're using nginx or another server) with the rule to make it happen.

If you're looking to go with more of a single page application style pattern, there are several to many other considerations to take into account.

Link to comment
Share on other sites

It's not a big issue, but in the end, it do want something that is decent.  I am learning, so I'm sure the whole Idea of this is going to be a rewrite, so I'm just learning all the core work before I jump into more advanced frame works.  Right now I'm just trying to get comfortable with OOP with the MVC model.

So I'm basically writing a practice app that I hope will be able to help me in my full time job managing hvac and these are my goals.

  • Use MVC Model
  • Keep logic from display seperate
  • Keep public accessible pages categorized per option.

Example, I want customers.php to handle most customer front in display, jobs.php handle all jobs display etc.

I guess I could seperate it into more multiple files, but my attempt is to keep similar tasked group so I can manage it better.

So either way, I'm always looking for a new way to do something better within the scope of my own  understanding, because I'm still a novice.

Link to comment
Share on other sites

Not gonna lie, I didn't realize Smarty was still in active development but looking at the github repo it clearly is, so if it's something you're invested in it doesn't seem like a lost cause. As I said, it's been a bit since I've explored it - I moved to Twig for a bit, then my jobs moved me to Laravel with Blade and/or Vue.

All that aside, whatever template language you're using won't affect your routes. I'm sure others will kick in on this, but if you're just learning and starting fresh and you want to adhere to MVC patterns while keeping things DRY and using pretty URLs for routing, it might not be a bad idea to go straight to a framework just so you don't have to recreate the wheel.

It's always good to understand the things a framework is doing at the base level, but honestly these days the most popular and most used frameworks take care of so much of the crap work that it's kinda worth learning backwards IMO. If you're working on updating an existing plain PHP code base then obviously ignore that opinion entirely; your situation is going to supersede any bullshit I may spout about the ease of adopting a framework. On the other hand, if you're planning on rewriting everything anyway, why not make it a bit easier on yourself?

There are several frameworks out there, so if you decide to go that route do some research. Laravel is the most popular PHP framework and once you get used to its opinions it is pretty easy to deal with and offers some nice sugar out of the box, but there's a lot of magic there. In my experience CodeIgniter 4 is quite good, but offers less default functionality (and thereby less magic). I've heard good things about the latest version of Cake though I've never tried it. And from early experience with the latest major version and looking at the repo, Yii is ... not awesome. However, maybe that's changed?

Link to comment
Share on other sites

20 hours ago, maxxd said:

Not gonna lie, I didn't realize Smarty was still in active development but looking at the github repo it clearly is, so if it's something you're invested in it doesn't seem like a lost cause. As I said, it's been a bit since I've explored it - I moved to Twig for a bit, then my jobs moved me to Laravel with Blade and/or Vue.

All that aside, whatever template language you're using won't affect your routes. I'm sure others will kick in on this, but if you're just learning and starting fresh and you want to adhere to MVC patterns while keeping things DRY and using pretty URLs for routing, it might not be a bad idea to go straight to a framework just so you don't have to recreate the wheel.

It's always good to understand the things a framework is doing at the base level, but honestly these days the most popular and most used frameworks take care of so much of the crap work that it's kinda worth learning backwards IMO. If you're working on updating an existing plain PHP code base then obviously ignore that opinion entirely; your situation is going to supersede any bullshit I may spout about the ease of adopting a framework. On the other hand, if you're planning on rewriting everything anyway, why not make it a bit easier on yourself?

There are several frameworks out there, so if you decide to go that route do some research. Laravel is the most popular PHP framework and once you get used to its opinions it is pretty easy to deal with and offers some nice sugar out of the box, but there's a lot of magic there. In my experience CodeIgniter 4 is quite good, but offers less default functionality (and thereby less magic). I've heard good things about the latest version of Cake though I've never tried it. And from early experience with the latest major version and looking at the repo, Yii is ... not awesome. However, maybe that's changed?

Well I do plan on picking up some frameworks eventually.  I know a lot of people always criticize me for reinventing the wheel, but I enjoy doing it.    I guess the reason why I'm like that, because when I tried to learn c++ it became to the point where I couldn't make anything worth making and understanding the OOP portion of it for me was a real stinger as well.  That was 10 years ago, and then I picked up perl and started to learn how to do things with it that was usable, but dealing with frameworks and template systems was still a pain in the ass.   Of course having a full time job prevented me from continuing learning, so now I picked up PHP and I hate how the code naturally allowed to be a total disaster, but I'm figuring it out and making it better every day and being better and building OOP projects.  I like building things from ground up, kinda like building cars you know.  Tear it down and redo it all.  But I do plan on sticking with it, picking up Javascripts, and possible another language too, but I'm going to get proficient in php because I believe I'll continue to use it for at minimum the next 7 years.

Link to comment
Share on other sites

  • Solution

Totally get that about reinventing things to understand them, so I'm not going to give you any crap for going down that road if you choose to.

I will say that given that and your plans to take up a framework later on, you may want to (at this time) look into Twig as a templating language instead of Smarty - I'm sure someone here will correct me if I'm wrong but the impression that I get is that Smarty isn't all that used any more, while several frameworks either support Twig or use a dialect based on it. Heck, from looking at the Smarty repo it looks like they've taken that in a more Twig-based direction.

Another choice is to simply use PHP in your HTML files and skip a templating language entirely; this is a technique still widely used in the wild and is obviously easily extensible in plain PHP. It's not an approach I personally recommend or use these days, but again my last four or five years worth of jobs have been Laravel based, so I've gotten rather used to that.

Rounding back to your original question, the benefit of using a framework is that a framework usually takes care of the front-controller aspect of things, and you won't have to do any extra work in the .htaccess file to get the pretty URLs it sounds like you were originally asking about (before I derailed the entire conversation - sorry about that).

Link to comment
Share on other sites

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.