Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Define callback function for use in template

edited February 2013 in Framework

Can it be in apps/app/lib/Anything-at-all.php? I'm getting Error: Call to undefined function

Comments

  • The file should be a valid PHP class name, and match the class declared inside. Unless it contains functions, but then you need to explicitly require it first.

  • Yeah, I want to define a function to use in a template like so: {{loop_value|my_function}}

  • You can define static methods for this too. For example:

    <?php // apps/demo/lib/DemoFilter.php
    
    class DemoFilter {
        public static reversed ($val) {
            return strrev ($val);
        }
    }
    

    Anywhere in your view templates you should be able to call {{loop_value|DemoFilter::reversed}} and it should work.

  • Thanks! I was missing the DemoFilter::

  • I'm having an odd issue.

    Here's the code:

    <script>
        $(function () {
            $.multi_image ({
                field: '#images',
                preview: '#images-preview',
                path: 'properties/{{name|Property::clean_image_name}}'
            });
            $.multi_file ({
                field: '#documents',
                preview: '#documents-preview',
                path: 'properties/{{name|Property::clean_image_name}}'
            });
            $('#browse-image').click (function () {
                $.filebrowser ({
                    thumbs: true,
                    title: '{"Choose an image"}',
                    set_value: '#main_image',
                    path: 'properties/{{name|Property::clean_image_name}}',
                    callback: function (file) {
                        $('#main_image-preview').attr ('src', file);
                    }
                });
                return false;
            });
        });
    </script>
    

    And here's the output:

    <script>
        $(function () {
            $.multi_image ({
                field: '#images',
                preview: '#images-preview',
                path: 'properties/
    geitz'
            });
            $.multi_file ({
                field: '#documents',
                preview: '#documents-preview',
                path: 'properties/geitz'
            });
            $('#browse-image').click (function () {
                $.filebrowser ({
                    thumbs: true,
                    title: 'Choose an image',
                    set_value: '#main_image',
                    path: 'properties/geitz',
                    callback: function (file) {
                        $('#main_image-preview').attr ('src', file);
                    }
                });
                return false;
            });
        });
    </script>
    

    There's a newline between properties/ and geitz that chokes firefox's javascript parser.

    There's no newline the second or third time I call the function. If I call the function earlier in the html, in a comment for example, then the first path: 'properties/geitz' works as expected.

  • Hmm, if you replace the filter with var_dump, does it show any space/newline character? You could also try adding a trim() call in the filter function to make sure there's no extra space.

  • var_dump: path: 'properties/string(5) "Geitz"

    trim() doesn't change it.

  • That is odd. Can you post what the compiled template looks like? They're in the cache folder, named in the form appname-templatename.php

  • I figured it out. There was a newline after the closing ?> in my lib/Property.php. Once I took that out, the problem went away.

    (The cached template looked fine: path: 'properties/<?php echo Property::clean_image_name ($data->name); ?>')

Sign In or Register to comment.