Array to snippet problem

hello,

first i write into json file

$categoryData['exercises'][] = array(
            'pdfs' => $randomExercise->documents()->filterBy("extension", "pdf")->pluck('url', ',',),
            'vids' => $randomExercise->videos()->filterBy("extension", "mp4")->pluck('url', ',',),

…etc…

then i read the json file to output:

$categoryData = json_decode($jsonData, true);

then i put exercises of that $categoryData in a variable

$exercises = $categoryData['exercises'];

$exercises is a subarray in $categoryData.
then i loop through the exercises.

the following code works… but only when not in snippet:

<?php foreach ($exercises as $exercise) { ?>
                    
<p><?= $exercise['title']['value'] ?></p>
                      
<?php foreach ($exercise['vids'] as $videoUrl) : ?>
                              <div class="video">
                              <video controls><source src="<?= $videoUrl ?>" type="video/mp4"></video>
                              </div>
                         <?php endforeach; ?>
                       
<?php foreach ($exercise['pdfs'] as $pdfUrl) : ?>
                              <div class="pdf">
                                        <a href="<?= $pdfUrl ?>" target="_blank">[.pdf] 
                                    <?= getFilenameFromUrl($pdfUrl) ?></a>
                              </div>
                         <?php endforeach; ?>
<?php endforeach; ?>

now i want to put the vids and pdf loops in a snippet, but then it stops working when i do:

snippet('vidsAndPdfs', 'exercise' => $exercise);

also tried

<?php snippet('vidsAndPdfs', ['videoUrls' => $exercise['vids']]) ?>

in the snippet

<?php print_r($videoUrls); ?>

returns nothing.

how can i pass the vids and pdf to the snippet? cant figure it out.

regards
fusi

The HTML markup if the list block is directly stored in the content, so cannot be changed via the block snippet.

This is acutally the correct syntax, because now your variables are in an array. The first example was not proper syntax.

Does the snippet print anything when you just do a

<?= 'Hello, I\'m your snippet' ?>

I’d stay away from camelCase file names, always use lowercase. so your snippet filename should be vidsandpdf (or with dashes or underscores), and then

<?php snippet('vidsandpdfs', ['videoUrls' => $exercise['vids']]) ?>

Blockquote Does the snippet print anything when you just do a

<?= 'Hello, I'm your snippet' ?>

noooooo… :face_with_peeking_eye:

its dawning on me that the reason could be that all this happens inside this function

function displayJsonFile($jsonFile, $site, $kirby) {....

a fact i completely forgot about while in coding trance…

thats it.
thats the reason, isnt it?

I don’t know, you didn’t post your function

And I had a syntax error in this code

which I corrected above.

sorry, this is the function:

<?php function displayJsonFile($jsonFile, $site, $kirby) {
     
   
     $jsonData = file_get_contents($jsonFile);
     $categoryData = json_decode($jsonData, true);
     
     
      if ($categoryData !== null) {
           
                  $exercises = $categoryData['exercises'];           
                       
         foreach ($exercises as $exercise) { ?>                 
                   
                    <article>
                        <h2><?= $exercise['title']['value'] ?></h2>
                        <span class="dauer"><?= $exercise['dauer']['value'] ?> Minuten</span><br>
                        <p><?= $exercise['text']['value'] ?></p>

                       

                      <?php snippet ('vidsandpdfs') ?>

                        <?php foreach ($exercise['vids'] as $videoUrl) : ?>
                              <div class="video">
                              <video controls><source src="<?= $videoUrl ?>" type="video/mp4"></video>
                              </div>
                         <?php endforeach; ?>
                         
                         <?php foreach ($exercise['pdfs'] as $pdfUrl) : ?>
                              <div class="pdf">
                                        <a href="<?= $pdfUrl ?>" target="_blank">[.pdf] <?= getFilenameFromUrl($pdfUrl) ?></a>
                              </div>
                         <?php endforeach; ?>
                   
                         
                    </article>
                   
                    
                <?php } ?>
                
               </div>
        <?php  } 
     
    
} ?>

the vidsandpdfs snippet i left in the function with the corrected hello snippet content, which doesnt show…

Ok, after

insert

dump($categoryData);

Maybe your condition returns false, so nothing happens after the if statement.


Wondering why you are outputting HTML via a function… :thinking:

Maybe your condition returns false, so nothing happens after the if statement.

i think it true, because without the snippet everthing works.

the dump:

Array
(
    [categoryTitle] => Array
        (
            [value] => Warm-Up
        )

    [categorySubtitle] => Array
        (
            [value] => 
        )

    [categoryTimeOut] => Array
        (
            [value] => 1
        )

    [categoryToggle] => 
    [exercises] => Array
        (
            [0] => Array
                (
                    [title] => Array
                        (
                            [value] => Coding on a sunday afternoon
                        )

                    [uuid] => ICmF4W8cSq6V2aM7
                    [panelUrl] => http://localhost/sunny/panel/pages/leet+coding+central
                    [editlink] => http://localhost/sunny/panel/pages/leet+coding+central
                    [text] => Array
                        (
                            [value] => 
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et ac


                        )

                    [vids] => Array
                        (
                            [0] => http://localhost/sunny/media/pages/richard/categories/yes/fujiyama/0b027927f9-1683804918/fujiyama_exercise.mp4
                        )

                    [pdfs] => Array
                        (
                        )

                    [audios] => Array
                        (
                        )

                    [dauer] => Array
                        (
                            [value] => 2
                        )

                    [showMe] => 1
                    [showFlags] => 
                )

        )

)

Wondering why you are outputting HTML via a function…

i obeyed to chatgpt…

That might be a mistake…

But ok, let’s simplify it again:

<?php function displayJsonFile($jsonFile, $site, $kirby) {

	snippet ('test');

}

Where test just contains the string from above.

output successful!

Great, then add your original code one by one, starting with the first two lines.

sorry, so you mean the code in the snippet? that originally started with

<?php foreach ($exercise['vids'] as $videoUrl) : ?>
                              <div class="video">
                              <video controls><source src="<?= $videoUrl ?>" type="video/mp4"></video>
                              </div>
                         <?php endforeach; ?>

or do you mean code of the function?

I meant the code in the function.

wth it works now, but i dont know why…
snippet:

<?php foreach ($exercise['vids'] as $videoUrl) : ?>
                              <div class="video">
                              <video controls><source src="<?= $videoUrl ?>" type="video/mp4"></video>
                              </div>
                         <?php endforeach; ?>
                         
<?php foreach ($exercise['pdfs'] as $pdfUrl) : ?>
                              <div class="pdf">
                                        <a href="<?= $pdfUrl ?>" target="_blank">[.pdf] <?= getFilenameFromUrl($pdfUrl) ?></a>
                              </div>
                         <?php endforeach; ?>
<?= 'Hello, I\'m your snippet' ?>

template:

<?php snippet ('test',  ["exercise" => $exercise]); ?>

omfg it was a combination of missing 's and wrong parameters. ( insert meme with guy destroying computer in office )

thank you so much for your support!!! greetz, fusi