Jump to content

12 identical templates but with a small piece of different code: can I use processwire to create a single template?


franciccio-ITALIANO
 Share

Recommended Posts

Hi everyone.
I've created 12 templates that are the same but each with an extra bit of html code. 
The piece of code is as follows:

						<div>
							<div class="box-pf">
								<i class="fa fa-map-pin fa-2x fa-red faa-pulse animated"></i>
								<a href="">
									<span class="uk-text-middle"><i>Sonchus oleraceus</i> 'Grespino degli Orti'</span></b>
								</a>
							</div>	
						</div>	

On the third line we read "fa-red."
I created 12 similar templates.
The first template has only one box with fa-red, the last template has 12 boxes with icons of 12 different colors.
So. is there any way to have only 1 template and add, if I want and when I want, a small or big, same or different piece of html code?

 

Link to comment
Share on other sites

There are a tons of ways to do what you're asking, so I'm just suggesting something based on your previous example.
In the template add a repeater field "repeater" and add to it 3 fields (name whatever you want, just stick to mine if you want copy/paste the code block below):

1) class_string
2) title
3) description

All of them are of type "text".

Fill them as you like.

In the template file then paste this code where you need them to be placed.

<?php foreach($page->repeater as $block): ?>
<div>
	<div class="box-pf">
		<i class="<?= $block->class_string ?>"></i>
		<a href=""><span class="uk-text-middle"><i><?= $block->title ?></i> '<?= $block->description ?>'</span></b></a>
	</div>	
</div>	
<?php endforeach; ?>

 

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

  • 7 months later...
On 1/26/2021 at 1:33 PM, 3fingers said:

There are a tons of ways to do what you're asking, so I'm just suggesting something based on your previous example.
In the template add a repeater field "repeater" and add to it 3 fields (name whatever you want, just stick to mine if you want copy/paste the code block below):

1) class_string
2) title
3) description

All of them are of type "text".

Fill them as you like.

In the template file then paste this code where you need them to be placed.

<?php foreach($page->repeater as $block): ?>
<div>
	<div class="box-pf">
		<i class="<?= $block->class_string ?>"></i>
		<a href=""><span class="uk-text-middle"><i><?= $block->title ?></i> '<?= $block->description ?>'</span></b></a>
	</div>	
</div>	
<?php endforeach; ?>

 

Hi fingers, since you said there are MANY ways to do what I was asking, could you suggest another way?
The one you suggested is fine, but if possible I would like to avoid the repeater module. Maybe there is a solution in pure php language or with pure processwire api?

Link to comment
Share on other sites

many ways...

1. use childpages instead of repeaters would be possible
2. repeater matrix (pro module)
3. pagetable module
4. editing the https://processwire.com/modules/fieldtype-events/     to your needs this is a example for repetable items in a small amount in a own fieldtype module
....this comes in the first minute of thinking about your question.
processwire API code goes the same as for repeaters with different sources of the content (get childpages instead of repeateritems and so on)...

kind regards mr-fan

  • Like 2
Link to comment
Share on other sites

35 minutes ago, franciccio-ITALIANO said:

well i wanted to avoid ANY special module, to find a solution with only generic php language, anyway i will use the repeater, still i didn't learn how to use page categories and child pages... 

Hi,

depending on why and/or where those pieces of code are displayed or not, it could be easy to do this with php
for example, if one of these piece of code is displayed when/if the page is a child of some page or template, easy to have a php if based on the parent template or id

another way to do this, safe if it is your website, meaning some education if it is a client's one... would be to use toggles for the template "display this... yes/no", once more a simple check on the toggle value with php, job done (i've used this solution for a client who wanted some pages to be published, not hidden => in the sitemap but not always in the menu

pw and php are great friends ?

have a nice day

  • Like 1
Link to comment
Share on other sites

On 9/12/2021 at 8:57 AM, franciccio-ITALIANO said:

well i wanted to avoid ANY special module, to find a solution with only generic php language, anyway i will use the repeater, still i didn't learn how to use page categories and child pages... 

knowing page categories and child pages - or the usage of templates and pages as contentcontainers for everthing you ned - is essestiel!!

this is a great startpoint for learn this very very important thing - processwire manage the content you need - with simple PHP solutions you get get the content in your templates...i created a complete blog logic without a special module or something else - but you have to know to use pages and fields and learn this things.

 

  • Like 2
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...