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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현