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

Placing view template outside of the 'Views' dir when using view() #180

Closed
sayonara90s opened this issue Jul 19, 2016 · 3 comments
Closed

Comments

@sayonara90s
Copy link
Contributor

Dear Council...

I have a question.

Can I place view template outside Views dir when I am using view() function?

I have dir structure like this:

application/
.. Widgets/
.. .. Calendar/
.. .. .. Control.php
.. .. .. view.php

or

application/
.. OtherViews/

My problem:

Normally (getting an error), the path sould be reference:

.../application/Views/App\Widgets/calendar.php

related: System/Common.php line 146 - 151

        $file = $this->viewPath.$view;

        if (! file_exists($file))
        {
            $file = $this->loader->locateFile($view, 'Views');
        }

And my feature request:

I like the third param of view(), as far as I know that supported only one variable key (bool) $options['saveData'].

view(string $name, array $data, array $options)

I think wit adding 'default dir' can be solve the problem and optional.

$options = [
    'saveData' => true, // default 'not exists'
    'defaultDir' => 'Views' // default 'Views', this is my request... :)
];

With 'defaultDir' key, optional values:

  1. false or null, if I don't want to use 'Views' dir.
  2. dirname (string), for specific directory (I think this is not necessary).

Exm:

<?php namespace App\Widgets;

class Calendar {

    public function get()
    {
        return view('App\Widgets\Calendar\view', [], ['defaultDir' => '']);

        # ../application/Widgets/Calendar/view.php
    }

}

My widget combined with view_cell() (I like this feature, thanks!):

// non controller class
view_cell('App\Widgets\Calendar\control::get');

I think view() only work for the controller class (correct me if I'm wrong).
And, because it works perfect with my Blog module:

    application/
    modules/
    .. Blog/
    .. .. Controllers/
    .. .. Views/
    .. .. .. home.php
    view('Modules\Blog\home');

Hope you know what I mean.

Thanks!

@sayonara90s
Copy link
Contributor Author

I extended with my own (a little modification)

<?php namespace App\Widgets\src;

class BaseView extends \CodeIgniter\View\View {

    public function _view(string $name, array $data=[], array $options=[])
    {
        $saveData = false;

        if (array_key_exists('saveData', $options) && $options['saveData'] === true)
        {
            $saveData = (bool)$options['saveData'];
            unset($options['saveData']);
        }

        $this->setData($data, 'raw');

        return $this->render('App\Widgets\\' . $name, $options, $saveData);
    }

    public function render(string $view, array $options=null, $saveData=false): string
    {
        $start = microtime(true);

        $view = str_replace('.php', '', $view).'.php';

        // Was it cached?
        if (isset($options['cache']))
        {
            $cacheName = $options['cache_name'] ?: str_replace('.php', '', $view);

            if ($output = cache($cacheName))
            {
                $this->logPerformance($start, microtime(true), $view);
                return $output;
            }
        }

        $file = $this->viewPath.$view;

        if (! file_exists($file))
        {
            // Removed the second param
            $file = $this->loader->locateFile($view);
        }

        // ......
    }

}

@lonnieezell
Copy link
Member

The directory option isn't a bad option to add (there's a 'cache' option available too), but might not be necessary for this. If your view is within a folder that's namespaced, you can treat it like a namespaced file.

I think, though, it might be simpler just to get rid of the automatic folder portion and force the path to be full. So, no more view('Modules\Blog\home'), but a more explicit (and flexible) view('Modules\Blog\Views\home').

Does that make sense?

@sayonara90s
Copy link
Contributor Author

Yeah, I think I forgot something.... thanks! Good joob! :)

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

2 participants