flask-classful
flask-classful copied to clipboard
Can I specify a unique templates directory for each class?
Is it possible to specify a directory to render templates from? This would be analogous to Blueprints template_folder argument.
I would love that! It could even automatically find templates/class_name/ folder and use that! Also, if someone is looking into this, how about even rendering template of the same name as the method if not stated other way?
So, I've been poking around in something like this last few days, and made it work (but it's quite unpythonic).
Full version, supporting automatic template name via method name looks like this:
class ExtendedFlaskView(FlaskView):
"""docstring for ExtendedFlaskView"""
def template(self, template_name=None, **kwargs):
# Template name is given from view and method names if not provided
calling_method = inspect.stack()[1].function
if template_name is None:
template_name = (
self._get_attribute_name() + "s/" + calling_method + ".html.j2"
)
return render_template(template_name, **kwargs)
def _get_attribute_name(self):
model_name = type(self).__name__.replace("sView", "")
snake_model_name = re.sub("(?!^)([A-Z]+)", r"_\1", model_name).lower()
attribute_name = snake_model_name
return attribute_name
Here's simpler version, just filling template folder name:
class ExtendedFlaskView(FlaskView):
"""docstring for ExtendedFlaskView"""
def template(self, template_name, **kwargs):
template_name = ({}"s/{}".format(self._get_attribute_name(), template_name))
return render_template(template_name, **kwargs)
def _get_attribute_name(self):
model_name = type(self).__name__.replace("sView", "")
snake_model_name = re.sub("(?!^)([A-Z]+)", r"_\1", model_name).lower()
attribute_name = snake_model_name
return attribute_name
I guess it should be possible to make it much cleaner by providing a template folder name as view parameter, something like this:
class ExtendedFlaskView(FlaskView):
"""docstring for ExtendedFlaskView"""
def template(self, template_name, **kwargs):
# Template name is given from view and method names if not provided
template_name = "{}/{}".format(self.template_folder, template_name)
return render_template(template_name, **kwargs)
I would like to hear some of core contributors take on this, before trying to write PR.
@janpeterka We've recently moved this project into the Pallets Community Ecosystem, which provides community maintenance for popular Flask extensions. I've seen you writing thorough answers on a lot of issues in this repository. Are you interested in being a maintainer? There are no time obligations, but the more interested people involved the better.
@davidism Wow, thanks! What does being a maintainer entails? I will be glad to help this project if I'm able to :)
You'll have write permission, so you can merge PRs, close issues, etc. Releases require sign off by someone with release permission, but are otherwise automated, so community maintainers just need to ping us when it's time for a release. Join our Discord server to chat about it.
Ok, joined Discord, and will be happy to be added as moderator :)