3

I'm having a problem getting my asset URLs to print properly in templates. The site is using a multi-environment configuration (dev/stage/live) with localized content (en/es/da) and shared assets. Here are the config and settings.

In the config file:

define('SITE_URL', URI_SCHEME . $_SERVER['SERVER_NAME'] . '/');
define('BASEPATH', realpath(CRAFT_BASE_PATH . '/../') . '/');

return array(
    '*' => array(
        // common config settings
    ),
    'site.craft.dev' => array(
        'siteUrl' => array(
            'en' => 'http://site.craft.dev:8888/',
            'es' => 'http://site.craft.dev:8888/es/',
            'da' => 'http://site.craft.dev:8888/da/',
        ),
        'environmentVariables' => array(
            'baseUrl'  => 'http://site.craft.dev:8888/',
            'basePath' => BASEPATH . 'http/',
        ),
    ),
    'stage.site.com'  => array(
        // etc
    ),
    'site.com'  => array(
        // etc
    ), 
);

In the CP:

I have an 'Images' asset source defined with:

  • File System Path: {basePath}assets/img/

  • URL: /assets/img/

I have a 'printImage' asset field defined with:

  • Upload Location: Images (selected in dropdown); prints (in field)

Uploading images through the CP works great, and images go to the correct folder, and are viewable in the CP.

In the template (and here is the problem), when I request the image url using {{ printImage.url }} it outputs:

when it should be:

Also, when I switch to another language it outputs:

It seems to be missing the portion of the path defined in Settings:Assets:Sources:'Images', and also is including the localization prefix.

All other paths and URLs in the site are working perfectly. Anyone have any ideas where I'm going wrong with assets? I guess I don't understand how it's crafting the asset URLs. Should I be defining an 'assetsPath' in my environment variables?

Thank you!

| improve this question | | | | |
  • This is a great question and it comes up in my searches all the time. I only wish you had waited for an answer. There's no canonical documentation (that I know of) for setting asset paths in Craft and it's a major source of error and frustration for me. – David Rhoden Jun 9 '17 at 9:58
2

Everything is set up properly after all. I just wasn't referencing the actual image, but rather its parent record. Correct syntax in this context should have been:

{{ print.printImage.first().getUrl() }}

Feel free to delete this post, or leave it for the config details.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.