I’m very new to php and even if i try my best i’m facing an issue i’m not resolving for hours now.
Here is a template section :
<section class="yaml yaml-conferences">
<h1>Les conférences :</h1>
<?php $conferences = yaml($page->conferences()) ?>
<?php foreach($conferences as $conference): ?>
<article class="listitem">
<img src="<?php echo $page->image($conference['logoimg'])->url() ?>" alt="<?php echo $conference['nom'] ?>" />
<h2><?php echo $conference['nom'] ?></h2>
<p>Se déroulera le : <?php echo $conference['date'] ?> à <?php echo $conference['heure'] ?></p>
<p><?php echo $conference['intervenant'] ?></p>
<p><?php echo $conference['presentation'] ?></p>
</article>
<?php endforeach ?>
</section>
And then a chunk of my blueprint :
logo:
label: Image associée ?
type: toggle
text: on/off
width: 1/2
logoimg:
label: Nom de l'image
type: text
help: Utilisez le nom de l'image insérée précédemment dans le gestionnaire de fichiers de la page.
width: 1/2
My issue here is i want to conditionally load or not the logoimg with either the fact is the field is empty or not… OR via a toggle button in the structure field.
But, i tried the boolean method, i tried many things without any success… It seems any thing i can try is worthless. Please help.
I managed to make it “sorta work” but i feel the code is ugly and wrong in many ways, is someone here can help me? I used the toStructure instead of a yaml but i really don’t feel the difference and this might be overkill.
<section class="yaml yaml-conferences">
<h1>Les conférences :</h1>
<?php $conferences = yaml($page->conferences()) ?>
<?php foreach($conferences as $conference): ?>
<article class="listitem">
<?php foreach($page->conferences()->toStructure() as $verif): ?>
<?php if($verif->logo()->bool()): ?>
<img src="<?php echo $page->image($conference['logoimg'])->url() ?>" alt="<?php echo $conference['nom'] ?>" />
<?php endif ?>
<?php endforeach ?>
<h2><?php echo $conference['nom'] ?></h2>
<p>Se déroulera le : <?php echo $conference['date'] ?> à <?php echo $conference['heure'] ?></p>
<p><?php echo $conference['intervenant'] ?></p>
<p><?php echo $conference['presentation'] ?></p>
</article>
<?php endforeach ?>
</section>
Also, i don’t understand the purpose of the yaml() method, why using it while you can use toStructure() ? It seems to not break kirby features. (again, i’m totally naive in this subject, i am a front-end dev so…)
Well… i am dumb.
<section class="yaml yaml-conferences"><h1>Les conférences :</h1>
<?php $conferences = yaml($page->conferences()) ?>
<?php foreach($conferences as $conference): ?>
<article class="listitem">
<h2><?php echo $conference['nom'] ?></h2>
<?php if(!empty($conference['logoimg'])): ?>
<img src="<?php echo $page->image($conference['logoimg'])->url() ?>" alt="<?php echo $conference['nom'] ?>" />
<?php endif ?>
<p>Se déroulera le : <?php echo $conference['date'] ?> à <?php echo $conference['heure'] ?></p>
<p><?php echo $conference['intervenant'] ?></p>
<p><?php echo $conference['presentation'] ?></p>
</article>
<?php endforeach ?>
</section>
It work and this is even simplier than before.
Tried the “isset” but did not work, used an !empty
yaml
parses the data into an array structure, while toStructure
supports the Kirby method syntax.
It allows you to use if($conference->logoimg())
without using empty
for example.
But generally your new code looks good.
1 Like
Thanx, i learn a little more everyday ^^
So, why i see everywhere the yaml() method while toStructure seems to be more elegant?
If toStructure allow more usages i dont see why using yaml() to make an array is useful, it only make code more confusing to me (again, i’m a total newbie so, i miss something obviously).
yaml
is a Kirby feature for a longer time now. The toStructure
method has been added together with the “structure” Panel field not long ago.
yaml
can be useful because arrays are generally easier to parse if you don’t know the structure in advance.
This is exactly for what I’m looking for, but it doesn’t work
Part of my blueprint:
timefrom_am: label: Von type: time
Part of my snippet code:
<?php $var = "$item->timefrom_pm()"; #$var = "$openinghours->timefrom_pm()"; if($var) { echo "times"; }
Wether $var1 or $var2 it’s always the same (wrong) php output, but the data aren’t definitely the same:
Thanks for help!
Best,
Andreas
texnixe
December 12, 2015, 12:11am
8
Could you pls. post your complete code? What are the quotation marks around $item-> timeframe_pm()
etc. doing there ?
This is the complete code. I just changed the question marks cause of markdown. Now everything is like in my code:
My content file: openinghours.txt
Title: Öffnungszeiten
----
Openinghours:
-
weekday: Montag
timefrom_am: 09:00
timeto_am: 12:00
timefrom_pm: 14:00
timeto_pm: 18:00
-
weekday: Dienstag
timefrom_am: 09:00
timeto_am: 12:00
timefrom_pm: 14:00
timeto_pm: 18:00
-
weekday: Mittwoch
timefrom_am: 09:00
timeto_am: 12:00
timefrom_pm:
timeto_pm:
My blueprint openinghours.php
<?php if(!defined('KIRBY')) exit ?>
title: Page
fields:
title:
label: Title
type: text
openinghours:
label: Öffnungszeiten
type: structure
entry: >
<h2>{{label}}</h2>
<time itemprop="openingHours" datetime="{{weekday}} {{timefrom_am}} {{timeto_am}}, {{timefrom_pm}} {{timeto_pm}}">
<span>{{weekday}}</span>
{{timefrom_am}} – {{timeto_am}} Uhr + {{timefrom_pm}} – {{timeto_pm}} Uhr
</time>
fields:
weekday:
label: Wochentag
type: select
options:
Montag: Montag
Dienstag: Dienstag
Mittwoch: Mittwoch
Donnerstag: Donnerstag
Freitag: Freitag
timefrom_am:
label: Von
type: time
width: 1/2
timeto_am:
label: Bis
type: time
width: 1/2
line:
type: line
timefrom_pm:
label: Von
type: time
width: 1/2
timeto_pm:
label: Bis
type: time
width: ½
And my snippet opening hours.php
<?php $page = page('opening-hours'); ?>
<?php foreach($page->openinghours()->toStructure() as $item):
$var = '$item->timefrom_pm()';
?>
<time itemprop="openingHours" datetime="<?php echo $item->weekday() ?> <?php echo $item->timefrom_am(); ?> <?php echo $item->timeto_am(); ?>">
<span><?php echo $item->weekday() ?></span>
<?php echo $item->timefrom_am(); ?> – <?php echo $item->timeto_am(); ?> Uhr
<?php
if($var) {
echo "nothing"; ?>
+ <?php echo $item->timefrom_pm(); ?> – <?php echo $item->timeto_pm(); ?> Uhr
<?php
}
?>
</time>
<hr />
<?php endforeach ?>
Thanks a lot!
Best,
Andreas
You shouldn’t put quotes around your variable definitions, otherwise you get the literal string, not the value of the variable. Instead use this:
$var = $item->timefrom_pm();
texnixe
December 12, 2015, 10:59am
11
This will work:
<?php $page = page('openinghours'); ?>
<?php foreach($page->openinghours()->toStructure() as $item):
$var = $item->timefrom_pm()->value();
?>
<time itemprop="openingHours" datetime="<?php echo $item->weekday() ?> <?php echo $item->timefrom_am(); ?> <?php echo $item->timeto_am(); ?>">
<span><?php echo $item->weekday() ?></span>
<?php echo $item->timefrom_am(); ?> – <?php echo $item->timeto_am(); ?> Uhr
<?php
if($var) { echo "+ " . $item->timefrom_pm() . " – " . $item->timeto_pm() . " Uhr" ; } ?>
</time>
<hr />
<?php endforeach ?>
You guys are awesome!
Thanks for helping me out