Skip to content

Commit

Permalink
Merge 5763e80 into 6846f30
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Jun 26, 2018
2 parents 6846f30 + 5763e80 commit b76b1da
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions masonite/snippets/auth/controllers/LoginController.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
''' A Module Description '''
from masonite.facades.Auth import Auth


class LoginController(object):
''' Login Form Controller '''

Expand Down
37 changes: 25 additions & 12 deletions masonite/view.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader
from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader, ChoiceLoader
from jinja2.exceptions import TemplateNotFound
from masonite.exceptions import RequiredContainerBindingNotFound

Expand Down Expand Up @@ -30,6 +30,7 @@ def __init__(self, container):
self.cache_type = None

self.template = None
self.environments = []


def render(self, template, dictionary={}):
Expand Down Expand Up @@ -76,7 +77,7 @@ def _update_from_composers(self):
else:
# Add a slash to symbolize going into a deeper directory structure
compiled_string += '/'


def composer(self, composer_name, dictionary):
"""
Expand Down Expand Up @@ -128,30 +129,42 @@ def exists(self, template):
return True
except TemplateNotFound as e:
return False

def add_environment(self, template_location, loader=PackageLoader):
# loader(package_name, location)
# /dashboard/templates/dashboard
if loader == PackageLoader:
template_location = template_location.split('/')

self.environments.append(loader(template_location[0], '/'.join(template_location[1:])))
else:
self.environments.append(
loader(template_location))

def __load_environment(self, template):
self.template = template
self.filename = template + '.html'

if template.startswith('/'):
location = template.split('/')
# Filter blanks strings from the split
location = list(filter(None, template.split('/')))
self.filename = location[-1] + '.html'

# If the location is more than 1 directory deep
if len(location[1:-1]) > 1:
loader = PackageLoader(*location[1:-1])
else:
loader = FileSystemLoader(str('/'.join(location[1:-1]) + '/'))

loader = PackageLoader(location[0], '/'.join(location[1:-1]))

self.env = Environment(
loader=loader,
loader=ChoiceLoader(
[loader] + self.environments
),
autoescape=select_autoescape(['html', 'xml']),
extensions=['jinja2.ext.loopcontrols']
)
else:

self.env = Environment(
loader=PackageLoader('resources', 'templates'),
loader=ChoiceLoader(
[PackageLoader('resources', 'templates')] +
self.environments
),
autoescape=select_autoescape(['html', 'xml']),
extensions=['jinja2.ext.loopcontrols']
)
Expand Down
1 change: 1 addition & 0 deletions storage/templates/tests/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ test }}
1 change: 1 addition & 0 deletions storage/test_location.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ test }}
23 changes: 23 additions & 0 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from masonite.view import view, View
from masonite.exceptions import RequiredContainerBindingNotFound
import pytest
from jinja2 import FileSystemLoader, PackageLoader


class TestView:
Expand Down Expand Up @@ -83,6 +84,21 @@ def test_composers_with_wildcard_base_view(self):
view = self.container.make('View')
assert 'test_user' in view('mail/welcome').rendered_template

def test_composers_with_wildcard_base_view_route(self):
self.container.make('ViewClass').composer('mail*', {'to': 'test_user'})

assert self.container.make('ViewClass').composers == {'mail*': {'to': 'test_user'}}

view = self.container.make('View')
assert 'test_user' in view('mail/welcome').rendered_template

def test_render_deep_in_file_structure_with_package_loader(self):

self.container.make('ViewClass').add_environment('storage')

view = self.container.make('View')
assert view('/storage/templates/tests/test', {'test': 'testing'}).rendered_template == 'testing'

def test_composers_with_wildcard_lower_directory_view(self):
self.container.make('ViewClass').composer('mail/welcome*', {'to': 'test_user'})

Expand Down Expand Up @@ -116,6 +132,13 @@ def test_view_share_updates_dictionary_not_overwrite(self):

assert viewclass.dictionary == {'test1': 'test1', 'test2': 'test2'}

def test_adding_environment(self):
viewclass = self.container.make('ViewClass')

viewclass.add_environment('storage', loader=FileSystemLoader)

assert viewclass.render('test_location', {'test': 'testing'}).rendered_template == 'testing'

def test_view_throws_exception_without_cache_binding(self):
view = self.container.make('View')

Expand Down

0 comments on commit b76b1da

Please sign in to comment.