flask-classful icon indicating copy to clipboard operation
flask-classful copied to clipboard

Can I specify a unique templates directory for each class?

Open noslenkwah opened this issue 5 years ago • 6 comments

Is it possible to specify a directory to render templates from? This would be analogous to Blueprints template_folder argument.

noslenkwah avatar Feb 24 '20 20:02 noslenkwah

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?

janpeterka avatar May 27 '20 13:05 janpeterka

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 avatar Jun 01 '20 13:06 janpeterka

@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 avatar Sep 08 '23 02:09 davidism

@davidism Wow, thanks! What does being a maintainer entails? I will be glad to help this project if I'm able to :)

janpeterka avatar Sep 08 '23 10:09 janpeterka

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.

davidism avatar Sep 08 '23 14:09 davidism

Ok, joined Discord, and will be happy to be added as moderator :)

janpeterka avatar Sep 09 '23 07:09 janpeterka