RaiN3772 Posted November 10, 2021 Share Posted November 10, 2021 (edited) Hello there, i'm looking for a way to create a template system, i tried googling but nothing fancy shows up so my idea is to create a function where i can list the template name and its path in an array which should looks like this <?php function template($title) { $template = array( 'header' => include('inc/templates/header.php'), 'footer' => include('inc/templates/footer.php'), ); return $template[$title]; } ?> and in my index page i just call it with variable like <?=template('header');?> or {$header}, whatever works, but it seems like its duplicating my file contents, and some weird integer number at the bottom of the page, so is there an easy way that i can do or a tweak in my code? thanks Edited November 10, 2021 by RaiN3772 Quote Link to comment https://forums.phpfreaks.com/topic/314197-template-system/ Share on other sites More sharing options...
Solution maxxd Posted November 10, 2021 Solution Share Posted November 10, 2021 We don't know what the rest of your code looks like, so it's difficult to say what's actually happening. My biggest question is is there a reason you're not using one of the many existing PHP template engines? Personally, I'm partial to Twig when not in Laravel land. Quote Link to comment https://forums.phpfreaks.com/topic/314197-template-system/#findComment-1591907 Share on other sites More sharing options...
RaiN3772 Posted November 10, 2021 Author Share Posted November 10, 2021 1 hour ago, maxxd said: We don't know what the rest of your code looks like, so it's difficult to say what's actually happening. My biggest question is is there a reason you're not using one of the many existing PHP template engines? Personally, I'm partial to Twig when not in Laravel land. didnt know such things existed lol, im newbie to php world, thanks alot mate Quote Link to comment https://forums.phpfreaks.com/topic/314197-template-system/#findComment-1591908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.