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

Can I specify a unique templates directory for each class? #123

Open
noslenkwah opened this issue Feb 24, 2020 · 6 comments
Open

Can I specify a unique templates directory for each class? #123

noslenkwah opened this issue Feb 24, 2020 · 6 comments

Comments

@noslenkwah
Copy link

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

@janpeterka
Copy link
Contributor

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
Copy link
Contributor

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.

@davidism
Copy link
Member

davidism commented Sep 8, 2023

@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.

@janpeterka
Copy link
Contributor

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

@davidism
Copy link
Member

davidism commented Sep 8, 2023

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.

@janpeterka
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants