$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How to create dynamic templates?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to create dynamic templates?

matteradmin9PV0评论

So let's say that I have a navbar template with the following content:

// template_parts / navbar.php

<nav class="navbar navbar-expand-lg dark bg-transparent fixed-top" id="mainNav">
  <div class="container">
    <a class="navbar-brand" href="./">
     This is the company name
    </a>
  </div>
</nav>

Is there a way to pass a variable to the template in order to change, in this case, the company's name when calling the template?

Something like get_template_part("template_parts/navbar","Microsoft");

I know the above syntax works for another purpose, I'm just trying to illustrate what I want to achieve.

Is there a way to do this? Thanks beforehand!

So let's say that I have a navbar template with the following content:

// template_parts / navbar.php

<nav class="navbar navbar-expand-lg dark bg-transparent fixed-top" id="mainNav">
  <div class="container">
    <a class="navbar-brand" href="./">
     This is the company name
    </a>
  </div>
</nav>

Is there a way to pass a variable to the template in order to change, in this case, the company's name when calling the template?

Something like get_template_part("template_parts/navbar","Microsoft");

I know the above syntax works for another purpose, I'm just trying to illustrate what I want to achieve.

Is there a way to do this? Thanks beforehand!

Share Improve this question asked Feb 3, 2019 at 12:15 HimadHimad 5552 silver badges9 bronze badges 1
  • 2 Possible duplicate of Passing variables from header.php to template and vice verca – Jacob Peattie Commented Feb 3, 2019 at 12:26
Add a comment  | 

1 Answer 1

Reset to default 0

You can include the template like this include(locate_template('template-part.php'));. to pass the variable or used the outer variable/ dynamic values in the template.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far