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

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

19 hours ago, virtualgadjo said:

hi,

i'm probably a bit late... but i would have suggested to use markup regions which is imho the king of easy ways to do what you are looking for 🙂

have a nice day

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Joachim
      Long time user and huge fan of PW, but this time I can't find an answer to my question this time:
      For my social media buttons, I have a Repeater field called var_link_web with two fields: one is for the URL, and the other is an Images field containing two images that are used as a background-image for a <div>, of which the second is the ':hover' version (although activated through JavaScript here). 
      There are four instances of this Repeater, of which two are 'turned off'.
      I use the following PHP in my _main.php to call them, wrapped in <p><?php == ?></p>:
      $s_m_button = $variables->var_link_web; foreach($s_m_button as $button){ $button_image = $button->var_link_image->first->height(80); $button_image2 = $button->var_link_image->eq(1)->height(80); echo " <a href='$button->var_link_url'> <div class='image_link' style='background-image:url({$button_image->url})' onMouseOver='this.style.backgroundImage=url({$button_image2->url})' onMouseOut='this.style.backgroundImage=url({$button_image->url})'> </div> </a>"; }; ($variables leads to an unpublished page with several fields I want to have easy access to, and is defined in _init.php.)
      However, this gives me the following result:

      I have no idea where the extra <p>'s come from. The URL field has the 'HTML Entity Encoder' turned on. What's even weirder is that the HTML source file seemingly renders correctly:
      <p> <a href='https://www.facebook.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1045/icons-facebook-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square.0x80.png)"'> </div> </a> <a href='https://www.instagram.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1046/icons-instagram-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square.0x80.png)"'> </div> </a> </p> Removing the JavaScript has no effect. I'm probably missing something obvious, but am at a loss here.
       
      Thank you in advance!
    • By Zendex
      Hi,
      I am trying to make a site for a studio, and I wanted to set a specific hover effect for my text. I want the background of the text to be lit up in the full height of the nav bar but it doesn't do it. I showed the expected result in the png in the attached files. I will also set the code here below, the top part is a css reset.
       
      HTML:
      <!doctype html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>Zendex</title>
          <link href="style.css" rel="stylesheet" type="text/css">
          <link rel="preconnect" href="https://fonts.gstatic.com"> 
      <link href="https://fonts.googleapis.com/css2?family=Offside&display=swap" rel="stylesheet">
      </head>
      <body>
          <div id="nav_bar">
              <div id="Zendex">ZENDEX</div>
      <ul>
          <a href="contact.html"><li>CONTACT</li></a>
          <a href="films.html"><li>FILMS</li></a>
          <a href="about.html"><li>ABOUT</li></a>
          <a href="home.html"><li>HOME</li></a>
              </ul>
          </div>
          <div class="main"></div>
      </body>
      </html>
      CSS:
      @charset "UTF-8";
      /* CSS Document */
      html, body, div, span, applet, object, iframe,
      h1, h2, h3, h4, h5, h6, p, blockquote, pre,
      a, abbr, acronym, address, big, cite, code,
      del, dfn, em, img, ins, kbd, q, s, samp,
      small, strike, strong, sub, sup, tt, var,
      b, u, i, center,
      dl, dt, dd, ol, ul, li,
      fieldset, form, label, legend,
      table, caption, tbody, tfoot, thead, tr, th, td,
      article, aside, canvas, details, embed, 
      figure, figcaption, footer, header, hgroup, 
      menu, nav, output, ruby, section, summary,
      time, mark, audio, video {
          margin: 0;
          padding: 0;
          border: 0;
          font-size: 100%;
          font: inherit;
          vertical-align: baseline;
      }
      article, aside, details, figcaption, figure, 
      footer, header, hgroup, menu, nav, section {
          display: block;
      }
      body {
          line-height: 1;
          background-color: #6A6969;
      }
      ol, ul {
          list-style: none;
      }
      blockquote, q {
          quotes: none;
      }
      blockquote:before, blockquote:after,
      q:before, q:after {
          content: '';
          content: none;
      }
      table {
          border-collapse: collapse;
          border-spacing: 0;
      }
      /*-------------------------------------------------------------------------------------------------------------------------------------------------*/
      #nav_bar{
          overflow: hidden;
          position: fixed;
          top: 0;
          width: 100vw;
          height: 125px;
          background-color: #484848;
          border: solid 1px black;
      }
      li{
          font-size: 24px;
          text-shadow: 1px 1px 100px white;
          font-family: 'Offside', cursive;
          float: right;
          margin: 45px 50px 0px 0px;
          height: 125px;
      }
      #Zendex{
          font-size: 24px;
          text-shadow: 1px 1px 100px white;
          font-family: 'Offside', cursive;
          float: left;
          margin: 45px 0px 0px 100px;
          color: azure;
      }
      #home{
          
      }
      #about{
          
      }
      #films{
          
      }
      #contact{
          
      }
      ul{
          margin: 0px 50px 0px 0px;
          color: azure;
          height: 125px;
      }
      a:link{
          color: white;
      }
      li:hover{
          height: 125px;
          background-color: #6A6969;
          color: white;
      }
       

    • By hellomoto
      I can't tell what's wrong; my local development version appears just fine, but I copy over the site files and db online and the homepage content is not being contained. This is what it should look like (same site in the same browser, running on my localhost): http://imgur.com/UFZFzrd
      What could be the problem here? Sorry to bring up something so irrelevant to PW here, I just know that you all are a valiant and helpful group, and no one on StackExchange seems to even know what I'm talking about.
      Thanks a lot.
    • By DooM
      Hey guys,
      I'm using Form builder module by Ryan and I'm trying to render HTML success message after form is sent using value from this field:

      but it renders exactly as it is written in the field, not as HTML. To render it I just use echo
      <?= $successMessage ?> What am I missing?
       
      Thanks for any help. 🧒
    • By Marcel
      Hey, 
      - we made a page as admins
      - as admins each  <img> tag is loaded and images are displayed
      - we tested the page as a pre-definded test user which is "guest" (Admin Theme: Reno)
      - as test user each <img> is missing and so no image is displayed
      I checked this in dev-mode on firefox and chrome. Does anyone have an idea or has had similar issues?
       
      Thank you in advance.
       
      Marcel
×
×
  • Create New...