base url for images in a package

Permalink
I am migrating a site to a C5 package and I wish to use the existing twig templates. To do so, I added the following twig() method to the page controller. report.php is just a standard twig template and ideally wouldn't have a PHP extension but C5 complains if it isn't. Then, I render to the C5 template page /twig which has a single line of code echo($html);.

<?php
class Report extends DashboardPageController
{
    private $twig;
    public function view($id)
    {
        $this->twig('dashboard/report.php', $this->getData($id));
    }
    //Actually, this is located in abstract parent
   protected function twig(string $template, array $variables=[], bool $render=true):string {
        if(!$this->twig) {
            $this->twig = new \Twig\Environment(new \Twig\Loader\FilesystemLoader(__DIR__.'/../single_pages'), [
                'debug'=>true,
                'strict_variables'=>true
            ]);


It seems to work well enough, however, any recommendations are certainly welcomed. The reason I described how I am using twig is I expect there is a standard C5 way of doing what I will be asking next, but it might not work with my twig solution

I have all these images located in packages/my_app/images and the browser url is index.php/dashboard/report. How can I use the images without hardcoding some long absolute path?

Thanks

 
linuxoid replied on at Permalink Best Answer Reply
linuxoid
Use this in the controller:
$pkg = $this->app->make(PackageService::class)->getByHandle('YOUR_PACKAGE_HANDLE');
$img_url = $pkg->getRelativePath() . '/images';
$this->set('img_url', $img_url);

The $img_url in view will be your relative path to the images folder:
<img src="<?php echo $img_url; ?>/picture.png" />
NotionCommotion replied on at Permalink Reply
Thanks linuxoid,

Felt kind of lazy and was hoping there was a single "defineBaseImageUrl()" method which would eliminate the need to edit each image src.

I also need to learn more about the app object as it seems to be a cornerstone of many needs. True? get_class_methods($this->app) provides the following

shutdown
dispatch
clearCaches
isInstalled
checkPageCache
handleAutomaticUpdates
setupPackageAutoloaders
setupPackages
setupFilesystem
isRunThroughCommandLineInterface
handleURLSlashes
handleCanonicalURLRedirection
environment
detectEnvironment
build
getRuntime
getRegisteredAliases
getRegisteredInstances
bindShared
when
bound
resolved
isAlias
bind
addContextualBinding
bindIf
singleton
share
extend
instance
tag
tagged
alias
rebinding
refresh
wrap
call
make
resolving
afterResolving
isShared
getBindings
forgetInstance
forgetInstances
flush
getInstance
setInstance
offsetExists
offsetGet
offsetSet
offsetUnset
__get
__set
linuxoid replied on at Permalink Reply
linuxoid
You can search for classes, properties, methods etc. here:https://documentation.concrete5.org/api/...
JohntheFish replied on at Permalink Reply
JohntheFish
There is a method on the package object that gets the package dir.