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

Move the special $CONTENT_TITLE and $LAYOUT_TITLE values to context variables #172

Open
ultraq opened this issue May 5, 2018 · 3 comments

Comments

@ultraq
Copy link
Owner

ultraq commented May 5, 2018

These custom strings are only available within the scope of the layout:title-pattern processor, but there have been instances where devs have wanted access to these values elsewhere (usually the whole title).

Would it be possible to convert these into variables that I add to the current processing context instead so they can be used elsewhere? In the process, removing the custom placeholder names and turning them to something accessible via an expression instead (eg: ${CONTENT_TITLE}), taking advantage of the expression language to construct titles and any other strings using them.

@ultraq ultraq added this to the Layout Dialect 3 milestone Mar 18, 2019
@justwiebe
Copy link

Will this ever be implemented?

@ultraq
Copy link
Owner Author

ultraq commented Jul 30, 2022

Gave this yet another try today, and I remembered why I left this one alone for so long...

In essence: a title can be anything. Developers aren't just putting text in between <title>...</title> tags:

  • they're using expressions with th:text/th:utext
  • or expression inlining
  • including it from other templates with th:include/th:insert
  • inserting their own HTML with <th:block> or other elements and using th:remove="tag"
  • running their own customer processors
  • and maybe more I haven't even thought of

The solution to handle all of the above was to put those "anything" parts in the correct places for the pattern given to layout:title-pattern, and then letting Thymeleaf process the result.

This does mean that the resulting title parts are no longer under the control of the layout dialect, and so being able to assign the results to template-wide context variables is not possible with the current suite of processors, and adding a new processor to be able to handle this feels very out-of-place with the layout dialect. As such, I'm going to close this issue.


For people coming to this in future and looking for ways to read the title, then some things that I thought of while attempting this:

  1. Use Thymeleaf's th:include processor with a fragment expression (th:include might be on its way out (Deprecate th:include thymeleaf/thymeleaf#405), so this solution may have a limited lifespan):
<p th:include="~{::title}">The title will end up here</p>
  1. Create your own model processor that you can add to the the <title> element to read the result after the layout dialect has placed each title part and Thymeleaf has processed it:
class TitleModelProcessor extends AbstractAttributeModelProcessor {

  @Override
  protected void doProcess(ITemplateContext context, IModel model, AttributeName attributeName,
    String attributeValue, IElementModelStructureHandler structureHandler) {

    def titleText
    model.accept(new AbstractModelVisitor() {
      @Override
      void visit(IText text) {
        titleText = text
      }
    })

    // Do whatever you want with the title now that you have it 🎉
    println("Title is ${titleText}")
  }
}

As for being able to discern the content and layout title parts, that will depend on your own application as you'll know how those are put together.

@ultraq ultraq closed this as completed Jul 30, 2022
@ultraq ultraq self-assigned this Jan 21, 2024
@ultraq
Copy link
Owner Author

ultraq commented Jan 21, 2024

Reopening as I've had some inspiration on how to tackle this 💡 (Some 6 years later! 😅)

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

No branches or pull requests

2 participants