How to make simple templating?In which Class are these 'blade functions' startSection() and stopSection()?How can I prevent SQL injection in PHP?Use 'class' or 'typename' for template parameters?Why can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?How do I make a redirect in PHP?How do I get PHP errors to display?How do I get a YouTube video thumbnail from the YouTube API?How Do You Parse and Process HTML/XML in PHP?How do I check if a string contains a specific word?How does PHP 'foreach' actually work?

Go Pregnant or Go Home

Is there an Impartial Brexit Deal comparison site?

Why Were Madagascar and New Zealand Discovered So Late?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Mapping a list into a phase plot

Will it be accepted, if there is no ''Main Character" stereotype?

Can criminal fraud exist without damages?

Is there a problem with hiding "forgot password" until it's needed?

Is there any reason not to eat food that's been dropped on the surface of the moon?

At which point does a character regain all their Hit Dice?

Was the picture area of a CRT a parallelogram (instead of a true rectangle)?

What is the opposite of 'gravitas'?

Star/Wye electrical connection math symbol

How was Earth single-handedly capable of creating 3 of the 4 gods of chaos?

Minimal reference content

Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

Finding all intervals that match predicate in vector

Failed to fetch jessie backports repository

Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?

Displaying the order of the columns of a table

Print name if parameter passed to function

Is a roofing delivery truck likely to crack my driveway slab?



How to make simple templating?


In which Class are these 'blade functions' startSection() and stopSection()?How can I prevent SQL injection in PHP?Use 'class' or 'typename' for template parameters?Why can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?How do I make a redirect in PHP?How do I get PHP errors to display?How do I get a YouTube video thumbnail from the YouTube API?How Do You Parse and Process HTML/XML in PHP?How do I check if a string contains a specific word?How does PHP 'foreach' actually work?













1















NOTICE: This is about plain PHP and I am trying to replicate what Laravel does with the Blade templating engine, but in a much simpler and trivial way.



If I make a simple layout in layout.php like this:



Notice the variable for pageTitle and where the content should be displayed



<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php $pageTitle ?? ''; ?></title>
</head>
<body>

<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>

HERE I WANT TO LOAD MY CONTENT

</body>
</html>


What I can't understand is how to "drop in" the content and show the pageTitle when I want to show a simple page that uses this layout. For example:



<?php require(app_views_path().'/layouts/app.view.php'); ?>
<?php $pageTitle = 'Welcome'; ?>

<h1>Welcome Page</h1>


and now this tag should be included and the $pageTitle variable set when page is opened.



How to produce this scenario?



What I have managed on my own is to just wrap around the page content and split the layout to _head and _foot part so it looks like this:



<?php require(app_views_path().'/layouts/_partials/_head.view.php'); ?>

<h1>Welcome Page</h1>

<?php require(app_views_path().'/layouts/_partials/_foot.view.php'); ?>


But the $pageTitle I don't know how to set it because it gets rendered before the page content and I don't want to send the page title through the controller (that would be an anti pattern).










share|improve this question



















  • 1





    You need to define <?php $pageTitle = 'Welcome'; ?> before the require.

    – AbraCadaver
    Mar 21 at 15:21











  • Set this <?php $pageTitle = 'Welcome'; ?> then require the page that contains this <title><?php $pageTitle ?? ''; ?></title> - think of include/require as cutting and pasting that code from the file, in that spot. So if you pasted that, you would not set the title at the end would you?

    – ArtisticPhoenix
    Mar 21 at 15:21
















1















NOTICE: This is about plain PHP and I am trying to replicate what Laravel does with the Blade templating engine, but in a much simpler and trivial way.



If I make a simple layout in layout.php like this:



Notice the variable for pageTitle and where the content should be displayed



<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php $pageTitle ?? ''; ?></title>
</head>
<body>

<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>

HERE I WANT TO LOAD MY CONTENT

</body>
</html>


What I can't understand is how to "drop in" the content and show the pageTitle when I want to show a simple page that uses this layout. For example:



<?php require(app_views_path().'/layouts/app.view.php'); ?>
<?php $pageTitle = 'Welcome'; ?>

<h1>Welcome Page</h1>


and now this tag should be included and the $pageTitle variable set when page is opened.



How to produce this scenario?



What I have managed on my own is to just wrap around the page content and split the layout to _head and _foot part so it looks like this:



<?php require(app_views_path().'/layouts/_partials/_head.view.php'); ?>

<h1>Welcome Page</h1>

<?php require(app_views_path().'/layouts/_partials/_foot.view.php'); ?>


But the $pageTitle I don't know how to set it because it gets rendered before the page content and I don't want to send the page title through the controller (that would be an anti pattern).










share|improve this question



















  • 1





    You need to define <?php $pageTitle = 'Welcome'; ?> before the require.

    – AbraCadaver
    Mar 21 at 15:21











  • Set this <?php $pageTitle = 'Welcome'; ?> then require the page that contains this <title><?php $pageTitle ?? ''; ?></title> - think of include/require as cutting and pasting that code from the file, in that spot. So if you pasted that, you would not set the title at the end would you?

    – ArtisticPhoenix
    Mar 21 at 15:21














1












1








1








NOTICE: This is about plain PHP and I am trying to replicate what Laravel does with the Blade templating engine, but in a much simpler and trivial way.



If I make a simple layout in layout.php like this:



Notice the variable for pageTitle and where the content should be displayed



<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php $pageTitle ?? ''; ?></title>
</head>
<body>

<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>

HERE I WANT TO LOAD MY CONTENT

</body>
</html>


What I can't understand is how to "drop in" the content and show the pageTitle when I want to show a simple page that uses this layout. For example:



<?php require(app_views_path().'/layouts/app.view.php'); ?>
<?php $pageTitle = 'Welcome'; ?>

<h1>Welcome Page</h1>


and now this tag should be included and the $pageTitle variable set when page is opened.



How to produce this scenario?



What I have managed on my own is to just wrap around the page content and split the layout to _head and _foot part so it looks like this:



<?php require(app_views_path().'/layouts/_partials/_head.view.php'); ?>

<h1>Welcome Page</h1>

<?php require(app_views_path().'/layouts/_partials/_foot.view.php'); ?>


But the $pageTitle I don't know how to set it because it gets rendered before the page content and I don't want to send the page title through the controller (that would be an anti pattern).










share|improve this question
















NOTICE: This is about plain PHP and I am trying to replicate what Laravel does with the Blade templating engine, but in a much simpler and trivial way.



If I make a simple layout in layout.php like this:



Notice the variable for pageTitle and where the content should be displayed



<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php $pageTitle ?? ''; ?></title>
</head>
<body>

<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>

HERE I WANT TO LOAD MY CONTENT

</body>
</html>


What I can't understand is how to "drop in" the content and show the pageTitle when I want to show a simple page that uses this layout. For example:



<?php require(app_views_path().'/layouts/app.view.php'); ?>
<?php $pageTitle = 'Welcome'; ?>

<h1>Welcome Page</h1>


and now this tag should be included and the $pageTitle variable set when page is opened.



How to produce this scenario?



What I have managed on my own is to just wrap around the page content and split the layout to _head and _foot part so it looks like this:



<?php require(app_views_path().'/layouts/_partials/_head.view.php'); ?>

<h1>Welcome Page</h1>

<?php require(app_views_path().'/layouts/_partials/_foot.view.php'); ?>


But the $pageTitle I don't know how to set it because it gets rendered before the page content and I don't want to send the page title through the controller (that would be an anti pattern).







php templates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 15:29







lewis4u

















asked Mar 21 at 15:14









lewis4ulewis4u

5,16484377




5,16484377







  • 1





    You need to define <?php $pageTitle = 'Welcome'; ?> before the require.

    – AbraCadaver
    Mar 21 at 15:21











  • Set this <?php $pageTitle = 'Welcome'; ?> then require the page that contains this <title><?php $pageTitle ?? ''; ?></title> - think of include/require as cutting and pasting that code from the file, in that spot. So if you pasted that, you would not set the title at the end would you?

    – ArtisticPhoenix
    Mar 21 at 15:21













  • 1





    You need to define <?php $pageTitle = 'Welcome'; ?> before the require.

    – AbraCadaver
    Mar 21 at 15:21











  • Set this <?php $pageTitle = 'Welcome'; ?> then require the page that contains this <title><?php $pageTitle ?? ''; ?></title> - think of include/require as cutting and pasting that code from the file, in that spot. So if you pasted that, you would not set the title at the end would you?

    – ArtisticPhoenix
    Mar 21 at 15:21








1




1





You need to define <?php $pageTitle = 'Welcome'; ?> before the require.

– AbraCadaver
Mar 21 at 15:21





You need to define <?php $pageTitle = 'Welcome'; ?> before the require.

– AbraCadaver
Mar 21 at 15:21













Set this <?php $pageTitle = 'Welcome'; ?> then require the page that contains this <title><?php $pageTitle ?? ''; ?></title> - think of include/require as cutting and pasting that code from the file, in that spot. So if you pasted that, you would not set the title at the end would you?

– ArtisticPhoenix
Mar 21 at 15:21






Set this <?php $pageTitle = 'Welcome'; ?> then require the page that contains this <title><?php $pageTitle ?? ''; ?></title> - think of include/require as cutting and pasting that code from the file, in that spot. So if you pasted that, you would not set the title at the end would you?

– ArtisticPhoenix
Mar 21 at 15:21













1 Answer
1






active

oldest

votes


















1














I'd suggest wrapping this in a function.



<?php
function load_view($view, $data = [])
extract($data);
require(app_views_path() . $view . ".view.php");



You can call it like this:



data = ["pageTitle" => "Welcome"];
load_view("layouts/app", $data);


Then in your view, you'll also need to echo something, which your original was not:



<title><?=$pageTitle ?? ''?></title>





share|improve this answer























  • Hmm this fixes getting the data from controller to the view... but my problem is the content that should be loaded like in Laravel with @yield Maybe you know how to overcome this? For example when page is loaded with return view_load('some_template'); I want to avoid that "sendwich" what I have in my question (HEAD| Content | FOOT). I hope you understand me.

    – lewis4u
    Mar 22 at 15:16











  • Not sure I follow you. Might be worth opening another question to focus on that part of it.

    – miken32
    Mar 22 at 15:18











  • How does laravel do this $__env->startSection('content') I can't find this function startSection() nor stopSection()

    – lewis4u
    Mar 22 at 15:20











  • stackoverflow.com/questions/55302990/…

    – lewis4u
    Mar 22 at 15:29










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283666%2fhow-to-make-simple-templating%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I'd suggest wrapping this in a function.



<?php
function load_view($view, $data = [])
extract($data);
require(app_views_path() . $view . ".view.php");



You can call it like this:



data = ["pageTitle" => "Welcome"];
load_view("layouts/app", $data);


Then in your view, you'll also need to echo something, which your original was not:



<title><?=$pageTitle ?? ''?></title>





share|improve this answer























  • Hmm this fixes getting the data from controller to the view... but my problem is the content that should be loaded like in Laravel with @yield Maybe you know how to overcome this? For example when page is loaded with return view_load('some_template'); I want to avoid that "sendwich" what I have in my question (HEAD| Content | FOOT). I hope you understand me.

    – lewis4u
    Mar 22 at 15:16











  • Not sure I follow you. Might be worth opening another question to focus on that part of it.

    – miken32
    Mar 22 at 15:18











  • How does laravel do this $__env->startSection('content') I can't find this function startSection() nor stopSection()

    – lewis4u
    Mar 22 at 15:20











  • stackoverflow.com/questions/55302990/…

    – lewis4u
    Mar 22 at 15:29















1














I'd suggest wrapping this in a function.



<?php
function load_view($view, $data = [])
extract($data);
require(app_views_path() . $view . ".view.php");



You can call it like this:



data = ["pageTitle" => "Welcome"];
load_view("layouts/app", $data);


Then in your view, you'll also need to echo something, which your original was not:



<title><?=$pageTitle ?? ''?></title>





share|improve this answer























  • Hmm this fixes getting the data from controller to the view... but my problem is the content that should be loaded like in Laravel with @yield Maybe you know how to overcome this? For example when page is loaded with return view_load('some_template'); I want to avoid that "sendwich" what I have in my question (HEAD| Content | FOOT). I hope you understand me.

    – lewis4u
    Mar 22 at 15:16











  • Not sure I follow you. Might be worth opening another question to focus on that part of it.

    – miken32
    Mar 22 at 15:18











  • How does laravel do this $__env->startSection('content') I can't find this function startSection() nor stopSection()

    – lewis4u
    Mar 22 at 15:20











  • stackoverflow.com/questions/55302990/…

    – lewis4u
    Mar 22 at 15:29













1












1








1







I'd suggest wrapping this in a function.



<?php
function load_view($view, $data = [])
extract($data);
require(app_views_path() . $view . ".view.php");



You can call it like this:



data = ["pageTitle" => "Welcome"];
load_view("layouts/app", $data);


Then in your view, you'll also need to echo something, which your original was not:



<title><?=$pageTitle ?? ''?></title>





share|improve this answer













I'd suggest wrapping this in a function.



<?php
function load_view($view, $data = [])
extract($data);
require(app_views_path() . $view . ".view.php");



You can call it like this:



data = ["pageTitle" => "Welcome"];
load_view("layouts/app", $data);


Then in your view, you'll also need to echo something, which your original was not:



<title><?=$pageTitle ?? ''?></title>






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 16:47









miken32miken32

24.2k95073




24.2k95073












  • Hmm this fixes getting the data from controller to the view... but my problem is the content that should be loaded like in Laravel with @yield Maybe you know how to overcome this? For example when page is loaded with return view_load('some_template'); I want to avoid that "sendwich" what I have in my question (HEAD| Content | FOOT). I hope you understand me.

    – lewis4u
    Mar 22 at 15:16











  • Not sure I follow you. Might be worth opening another question to focus on that part of it.

    – miken32
    Mar 22 at 15:18











  • How does laravel do this $__env->startSection('content') I can't find this function startSection() nor stopSection()

    – lewis4u
    Mar 22 at 15:20











  • stackoverflow.com/questions/55302990/…

    – lewis4u
    Mar 22 at 15:29

















  • Hmm this fixes getting the data from controller to the view... but my problem is the content that should be loaded like in Laravel with @yield Maybe you know how to overcome this? For example when page is loaded with return view_load('some_template'); I want to avoid that "sendwich" what I have in my question (HEAD| Content | FOOT). I hope you understand me.

    – lewis4u
    Mar 22 at 15:16











  • Not sure I follow you. Might be worth opening another question to focus on that part of it.

    – miken32
    Mar 22 at 15:18











  • How does laravel do this $__env->startSection('content') I can't find this function startSection() nor stopSection()

    – lewis4u
    Mar 22 at 15:20











  • stackoverflow.com/questions/55302990/…

    – lewis4u
    Mar 22 at 15:29
















Hmm this fixes getting the data from controller to the view... but my problem is the content that should be loaded like in Laravel with @yield Maybe you know how to overcome this? For example when page is loaded with return view_load('some_template'); I want to avoid that "sendwich" what I have in my question (HEAD| Content | FOOT). I hope you understand me.

– lewis4u
Mar 22 at 15:16





Hmm this fixes getting the data from controller to the view... but my problem is the content that should be loaded like in Laravel with @yield Maybe you know how to overcome this? For example when page is loaded with return view_load('some_template'); I want to avoid that "sendwich" what I have in my question (HEAD| Content | FOOT). I hope you understand me.

– lewis4u
Mar 22 at 15:16













Not sure I follow you. Might be worth opening another question to focus on that part of it.

– miken32
Mar 22 at 15:18





Not sure I follow you. Might be worth opening another question to focus on that part of it.

– miken32
Mar 22 at 15:18













How does laravel do this $__env->startSection('content') I can't find this function startSection() nor stopSection()

– lewis4u
Mar 22 at 15:20





How does laravel do this $__env->startSection('content') I can't find this function startSection() nor stopSection()

– lewis4u
Mar 22 at 15:20













stackoverflow.com/questions/55302990/…

– lewis4u
Mar 22 at 15:29





stackoverflow.com/questions/55302990/…

– lewis4u
Mar 22 at 15:29



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283666%2fhow-to-make-simple-templating%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript