Jump to content

What php code should I use to link the same html code?


Recommended Posts

Hi, I have created over twenty different templates for the same site.
In all of them, the same long piece of html code appears. 
Could someone suggest me the php code I need to write in the template files to link that piece of html code, which is always the same everywhere?

What strategies could I use to link it?

The problem is that this long piece of code that is identical everywhere, MAY change IN TIME. So, when I fix it, I would have to change it in all twenty template files.... 

 

Link to comment
Share on other sites

Create a new php file inside template folder (or a subdirectory of it, ex. "inc") and paste your html portion you want to include in all the other templates, save it.
Then, in all the templates, where you want that portion to appear just:

<?php include('path/to/partial.php'); ?> // replace the path with yours.

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Quote
Quote

the php code I need to write in the template files to link that piece of html code, which is always the same everywhere?

 

Many examples on the net
https://css-tricks.com/the-simplest-ways-to-handle-html-includes/
https://www.thoughtco.com/html-in-many-docs-with-php-3469181
https://www.phptutorial.net/php-tutorial/php-include-file/

  • Thanks 1
Link to comment
Share on other sites

22 hours ago, 3fingers said:

Create a new php file inside template folder (or a subdirectory of it, ex. "inc") and paste your html portion you want to include in all the other templates, save it.
Then, in all the templates, where you want that portion to appear just:

<?php include('path/to/partial.php'); ?> // replace the path with yours.

 

Or you could use the wireIncludeFile-function of ProcessWire. I prefer this over the PHP include-function. 😉

<?php

wireIncludeFile('path/to/partial', [
	"foo" => $foo
]);

 

  • Thanks 1
Link to comment
Share on other sites

On 7/23/2021 at 11:57 AM, AndZyk said:

Or you could use the wireIncludeFile-function of ProcessWire. I prefer this over the PHP include-function. 😉

<?php

wireIncludeFile('path/to/partial', [
	"foo" => $foo
]);

 

Hi, is there a specific reason why you prefer the wireIncludeFile-function of ProcessWire instead of the php include-function?

Then... the foo code part... what is it for?

Link to comment
Share on other sites

5 hours ago, franciccio-ITALIANO said:

Sorry I'm ignorant, can you elaborate on what you mean by "markup regions"?

Hi, as shown by jacmaes, there are great docs about those markup regions but in case they looked a bit hard to dive in at first sight, do not hesitate to tell me, i would write some simple examples for you to see how great and simple they actually are and what you can easily do with them, as usual with pw, from the simple replacement of header and footer by one single file to more "funny" things 🙂

have a nice day

Link to comment
Share on other sites

22 hours ago, franciccio-ITALIANO said:

Hi, is there a specific reason why you prefer the wireIncludeFile-function of ProcessWire instead of the php include-function?

Then... the foo code part... what is it for?

I prefer wireIncludeFile because of these points described by Ryan in his blog post:

Quote
  • They receive all of ProcessWire's API variables, and optionally your own specified variables (in the same way demonstrated above).
  • They assume a starting point in /site/templates/, unless you point them somewhere else.
  • They will only allow files that are somewhere within the directory structures started by /site/templates/, /site/modules/ or /wire/modules/ … adding a little more safety.
  • The .php file extension is assumed for any files you give it, so you can optionally omit it. Of course you can use a different extension if you want to, but just note that if the file has a .php extension, it's optional to specify it.

The foo variable is just an own variable you can pass and use in the included file. This way you can keep track where you added variables.

 

Of course Markup Regions are also awesome and I like to combine Markup Regions with wireIncludeFile. I use Markup Regions for the whole blueprint of the templates and wireIncludeFile to split up large junks of code. Occasionally I use field templates for repeating fields markup. ProcessWire offers many ways to structure your code if you want to. 😀

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

1 hour ago, szabesz said:

Hi,

very nice cheatsheet, i use markup regions for all my websites 🙂 i hope @franciccio-ITALIANO will have a look at your post and fall in love with them too 🙂

have a nice day

  • Like 1
Link to comment
Share on other sites

On 7/28/2021 at 3:50 PM, virtualgadjo said:

Hi, as shown by jacmaes, there are great docs about those markup regions but in case they looked a bit hard to dive in at first sight, do not hesitate to tell me, i would write some simple examples for you to see how great and simple they actually are and what you can easily do with them, as usual with pw, from the simple replacement of header and footer by one single file to more "funny" things 🙂

have a nice day

Well in fact this page is too much for me

https://processwire.com/docs/front-end/output/markup-regions/

if you have a summary maybe I understand better... but I don't want to stress you, probably I will use the wireincludefile function

Link to comment
Share on other sites

On 7/29/2021 at 9:34 AM, AndZyk said:

I prefer wireIncludeFile because of these points described by Ryan in his blog post:

The foo variable is just an own variable you can pass and use in the included file. This way you can keep track where you added variables.

 

Of course Markup Regions are also awesome and I like to combine Markup Regions with wireIncludeFile. I use Markup Regions for the whole blueprint of the templates and wireIncludeFile to split up large junks of code. Occasionally I use field templates for repeating fields markup. ProcessWire offers many ways to structure your code if you want to. 😀

I got it thank you very much.
I think it will use wireincludefile then.
As for the variable, however, excuse the great ignorance (I have not yet taken a php course) but... what is it exactly? A js/css/php class created and used elsewhere?

Link to comment
Share on other sites

Sorry for interrupting the usual programm and topic here but... 

@franciccio-ITALIANO maybe you want to look up a mentor/tutor here that helps you to maintain and build ProcessWire sites from either the ground up or even to maintain already built sites.

I know you from your past questions, requests and and posts in several other topics.

As much as I want to see someone helping you... the more I think you need someone that's guiding you from the basics of PHP to the basics and fundamentals of ProcessWire to the state you can work out ProcessWire sites from each and every state and upwards.

So... my questions for our awesome community is... who can help and assist @franciccio-ITALIANO to get to a state where he is able to maintain a basic ProcessWire-website?
Native italian speakers (is this even the correct way to write this?) go ahead... and let us know.

If there is noone who can assist... let me know @franciccio-ITALIANO... I'll put time aside to guide and assist you in some kind but in english. ;)

  • Like 8
Link to comment
Share on other sites

21 hours ago, franciccio-ITALIANO said:

As for the variable, however, excuse the great ignorance (I have not yet taken a php course) but... what is it exactly? A js/css/php class created and used elsewhere?

A variable is something you store information in. It could be a string, an array, an object or something else in PHP. This is basic PHP knowledge, so maybe you should take a PHP course first. 😉

In my example the variable was just for demonstration that you could pass variables with the wireIncludeFile-function from "file-a.php" to "include-b.php". But you don't have to use it at all.

 

21 hours ago, franciccio-ITALIANO said:

Now, if I write this in html file, by Notepad++, reading the code becomes complicated, so better to use php code include...

The only difference is to write wireIncludeFile instead of include. Here is a comparison:

<?php
include("path/to/partial.php");
wireIncludeFile("path/to/partial");

But you can use the include-function if you want to, its just a matter of preference. 😉

Regards, Andreas

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

22 hours ago, AndZyk said:

A variable is something you store information in. It could be a string, an array, an object or something else in PHP. This is basic PHP knowledge, so maybe you should take a PHP course first. 😉

In my example the variable was just for demonstration that you could pass variables with the wireIncludeFile-function from "file-a.php" to "include-b.php". But you don't have to use it at all.

 

The only difference is to write wireIncludeFile instead of include. Here is a comparison:

<?php
include("path/to/partial.php");
wireIncludeFile("path/to/partial");

But you can use the include-function if you want to, its just a matter of preference. 😉

Regards, Andreas

Ok thank you so much!

  • Like 2
Link to comment
Share on other sites

On 8/1/2021 at 10:56 PM, wbmnfktr said:

Sorry for interrupting the usual programm and topic here but... 

@franciccio-ITALIANO maybe you want to look up a mentor/tutor here that helps you to maintain and build ProcessWire sites from either the ground up or even to maintain already built sites.

I know you from your past questions, requests and and posts in several other topics.

As much as I want to see someone helping you... the more I think you need someone that's guiding you from the basics of PHP to the basics and fundamentals of ProcessWire to the state you can work out ProcessWire sites from each and every state and upwards.

So... my questions for our awesome community is... who can help and assist @franciccio-ITALIANO to get to a state where he is able to maintain a basic ProcessWire-website?
Native italian speakers (is this even the correct way to write this?) go ahead... and let us know.

If there is noone who can assist... let me know @franciccio-ITALIANO... I'll put time aside to guide and assist you in some kind but in english. 😉

Hi, a personal tutor I think is everyone's dream, but I don't have money to pay for one. So I think it's better for me to ask something on the forum sometime when it happens. Without pretension.
Someday maybe I'll take a php course, but I can't at the moment.
Hi!

Link to comment
Share on other sites

8 hours ago, franciccio-ITALIANO said:

but I don't have money to pay for one

You can find plenty of great free resources on the web to learn PHP but I think @wbmnfktr is right that some kind of upfront investment (either time and/or money) in learning the basics of PHP will help you a lot for working with ProcessWire 😉 

15 min: https://www.youtube.com/watch?v=ZdP0KM49IVk

5 hours: https://www.youtube.com/watch?v=OK_JCtrrv-c

 

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...