This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

simpltekonline20120
simpltekonline20120

I've seen many other posts regarding using PHP code in pages, but they either involve simple, 3-line scripts that can easily be translated into Twig or whatever, or the thread gets bumped with other people asking similar questions with no answers. So, here I am.

I need to have the following script run on a page in OctoberCMS AS IS:

$files = array();
$dir = opendir('/public/uploads/uploads/');
while(false != ($file = readdir($dir))) {
    if(($file != ".") and ($file != "..") and ($file != "index.php")) {
        $files[] = $file; // put in array.
    }   
}
closedir($dir);

natsort($files); // sort

foreach($files as $file) {
    $name = preg_replace('/\\.[^.\\s]{3}$/', '', $file);
    $fname = str_replace("_", " ", $name);
    $this["gallery"] = "<div class='portfolio-item'>
            <div class='portfolio-item-wm'>
                <span></span>
                <img src='/public/uploads/uploads/$file'>
            </div>
            <h4>$fname</h4>
        </div>";
}

I don't want to translate any of it into Twig, but I know that I might need to reference the script on the page using {{ varaible }} or something.

Hopefully someone can help me :/

Last updated

daftspunky
daftspunky

First, create a file in your theme's pages directory called gallery.htm. Then paste in the following contents:

url = "/gallery"
==
<?
function onStart()
{
    $files = array();
    $dir = opendir('/public/uploads/uploads/');
    while(false != ($file = readdir($dir))) {
        if(($file != ".") and ($file != "..") and ($file != "index.php")) {
            $files[] = $file; // put in array.
        }
    }
    closedir($dir);

    natsort($files); // sort

    $galleryHtml = '';

    foreach($files as $file) {
        $name = preg_replace('/\\.[^.\\s]{3}$/', '', $file);
        $fname = str_replace("_", " ", $name);
        $galleryHtml .= "<div class='portfolio-item'>
                <div class='portfolio-item-wm'>
                    <span></span>
                    <img src='/public/uploads/uploads/$file'>
                </div>
                <h4>$fname</h4>
            </div>";
    }

    $this['gallery'] = $galleryHtml;
}
?>
==

<h1>My awesome gallery</h1>

{{ gallery|raw }}

Then you can open the /gallery page in your website to see the results.

Last updated

simpltekonline20120
simpltekonline20120

All it produces is this:

==
***Mising image icon***
$fname
"; } $this['gallery'] = $galleryHtml; } ?> ==
Gallery

EDIT: Ah! I got it to work, had to put the code itself into the code tab rather than the markup tab. Thank You!

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.