Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly use asset extension? #315

Open
CodeUmair opened this issue Jun 13, 2023 · 2 comments
Open

How to properly use asset extension? #315

CodeUmair opened this issue Jun 13, 2023 · 2 comments

Comments

@CodeUmair
Copy link

I am trying to use the asset extension, I have a public directory with index and htaccess and an assets directory, I am trying to load assets, CSS, js, and images from this directory and nothing is working for me.

$engine->loadExtension(new League\Plates\Extension\Asset(BASE_PATH . '/public/assets', true));

@adeilsonaalima
Copy link

Same here.

Sorry for my english... anyway:

This is my controller:

<?php

namespace src\controllers;

use League\Plates\Engine;
use League\Plates\Extension\Asset;

class Web
{
    private $templates;

    public function __construct()
    {
        $this->templates = new Engine(__DIR__ . "/../views", "php");
        $this->templates->loadExtension(new Asset(__DIR__ . '/../assets/', false)); // or true
    }

    public function home(): void
    {
        echo $this->templates->render("register-and-login/home", [
            "title" => "Home | " . SITE,
            "users" => ["Adeilson", "Karol"]
        ]);
    }
}

And this is in my template:

<link rel="stylesheet" href="<?= $this->asset('/css/style.css') ?>">

If I change to a non-existent path it gives an error 500, but if it is correct it defines the following path when rendering:

<link rel="stylesheet" href="/css/style.css?v=1687988335">

And this error:

GET https://mysite.com.br/css/style.css?v=1687988335 net::ERR_ABORTED 404 (Not Found)

Why? Someone can help me?

@RawkBob
Copy link
Contributor

RawkBob commented Jul 5, 2023

<link rel="stylesheet" href="<?= $this->asset('/css/style.css') ?> should be <link rel="stylesheet" href="/assets<?= $this->asset('/css/style.css') ?> i think?

The asset extension doesn't return the full public path, so you still need to put that in the html, it you view the generated source and compare the generated url to the expected url you should see this - from your example the html does not include the public assets folder. I've created a pull request #318 to update the docs with the assets path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants