Forum

Thread tagged as: Question, Feathers

Select specific file from feather

Just curious if it is possible to retrieve a single file via options from a perch feather. For instance, via the script call perch_get_javascript();. I have a working feather that pulls out my scripts but I've always wondered if you can be more specfic.

I've hunted through the docs but was unable to find anything.

Mathew Doidge

Mathew Doidge 2 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't understand. Can you give an example?

Sure, so my feather looks like:

<?php

PerchSystem::register_feather('Wings');

class PerchFeather_Wings extends PerchFeather
{
    public function get_javascript($opts, $index, $count)
    {
        $out = array();

        if ( null !== getenv("DEVELOPER_ENV") && getenv("DEVELOPER_ENV") == 'development') {

            if (!$this->component_registered('script')) {
                $out[] = $this->_script_tag(array(
                    'src'=>'/dist/js/main.js',
                    'defer'=>'defer'
                ));

                $this->register_component('script');
            }

            if (!$this->component_registered('myotherscript')) {
                $out[] = $this->_script_tag(array(
                    'src'=>'/dist/js/main.js',
                    'defer'=>'defer'
                ));

                $this->register_component('myotherscript');
            }

        } else {

            $JSversion = filemtime($_SERVER['DOCUMENT_ROOT'].'/dist/js/main.min.js');

            if (!$this->component_registered('script')) {
                $out[] = $this->_script_tag(array(
                    'src'=>'dist/js/main.min.'.$JSversion.'.js',
                    'defer'=>'defer',
                ));
                $this->register_component('script');
            }

            if (!$this->component_registered('myotherscript')) {
                $out[] = $this->_script_tag(array(
                    'src'=>'/dist/js/main.js',
                    'defer'=>'defer'
                ));

                $this->register_component('myotherscript');
            }

        }

        return implode("\n\t", $out)."\n";
    }
}
?>

If I wanted to perhaps only pull out myotherscript on one page, is there a way via the call on the actual page to be selective about what script it's going to add to my page? Something like perch_get_javascript('myotherscript');

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sure, the first argument to perch_get_javascript() is typically an options array, and you can use that however you'd like within your code.

That's good news. Is there any of the old feathers that use the options array in such a manner that I could look at to get my started on getting it setup correctly in my own feather?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think the Sass one did. But this is basic PHP - testing for a value with an if statement.