You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Adriaan Joubert <ad...@gmail.com> on 2021/10/11 15:47:36 UTC

Rendering a tapestry component to a string

Hi,

We are starting to use a library called fancytree (
http://wwwendt.de/tech/fancytree/demo/index.html). For basic trees this
integrates really easily with tapestry.

However for tree tables the simplest way to render the trees client-side is
to ship the contents of the tree table down in a json structure. For simple
values this works well.

Ideally we would like to have tapestry components available in the tree
table. We could come up with complicated ways of doing this (hidden divs
for every cell etc), but it would be easy if we could render a tapestry
component to a string, and simply ship this client side as a field in the
json.

Does this sound feasible at all? I found
https://github.com/uklance/tapestry-offline, but is there a simpler
built-in way?

If anybody has any ideas that would be great.

Cheers,

Adriaan

Re: Rendering a tapestry component to a string

Posted by Adriaan Joubert <ad...@gmail.com>.
Oh excellent! Thank you very much!

Adriaan

On Thu, 14 Oct 2021 at 22:33, Thiago H. de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Mon, Oct 11, 2021 at 12:47 PM Adriaan Joubert <ad...@gmail.com>
> wrote:
>
> > Hi,
> >
>
> Hello, Adriaan!
>
>
> > We are starting to use a library called fancytree (
> > http://wwwendt.de/tech/fancytree/demo/index.html). For basic trees this
> > integrates really easily with tapestry.
> >
> > However for tree tables the simplest way to render the trees client-side
> is
> > to ship the contents of the tree table down in a json structure. For
> simple
> > values this works well.
> >
> > Ideally we would like to have tapestry components available in the tree
> > table. We could come up with complicated ways of doing this (hidden divs
> > for every cell etc), but it would be easy if we could render a tapestry
> > component to a string, and simply ship this client side as a field in the
> > json.
> >
> > Does this sound feasible at all? I found
> > https://github.com/uklance/tapestry-offline, but is there a simpler
> > built-in way?
> >
>
> tapestry-offline is great when you want to use Tapestry to generate some
> content outside of Tapestry-handled requests.
>
> For Tapestry-handled requests, you can use its own PartialTemplateRenderer
> service:
>
> https://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/PartialTemplateRenderer.html
> .
> It works for component and block instances.
>
> One example taken from one of the Tapestry internal testing apps:
>
> public class PartialTemplateRendererDemo
> {
>
>     @Inject
>     @Property
>     private Block someBlock;
>
>     @Inject
>     private PartialTemplateRenderer partialTemplateRenderer;
>
>     @InjectComponent
>     private BeanDisplay beanDisplay;
>
>     @Property
>     private Pojo object;
>
>     void setupRender() {
>         object = new Pojo();
>         object.setDate(new Date(21342345234444L));
>         object.setString(String.valueOf(System.currentTimeMillis()));
>         object.setInteger((int) (System.currentTimeMillis() % 234123));
>     }
>
>     public String getServiceRenderedBlock() {
>         return partialTemplateRenderer.render(someBlock);
>     }
>
>     public String getServiceRenderedComponent() {
>         return partialTemplateRenderer.render(beanDisplay);
>     }
>
>     public static class Pojo
>     {
>         private int integer;
>         private String string;
>         private Date date;
>
>         public int getInteger()
>         {
>             return integer;
>         }
>
>         public void setInteger(int i)
>         {
>             this.integer = i;
>         }
>
>         public String getString()
>         {
>             return string;
>         }
>
>         public void setString(String s)
>         {
>             this.string = s;
>         }
>
>         public Date getDate()
>         {
>             return date;
>         }
>
>         public void setDate(Date date)
>         {
>             this.date = date;
>         }
>
>     }
> }
>
>  <t:block id="someBlock">
> <!-- One component that generates a good amount of markup -->
> <t:beandisplay t:id="insideBlock" t:object="object"/>
> </t:block>
>
> <div id="original">
> <t:delegate to="block:someblock"/>
> </div>
>
> <div id="serviceRenderedBlock">
> <t:outputRaw value="serviceRenderedBlock"/>
> </div>
>
> <div id="originalBeanDisplay">
> <t:beandisplay t:id="beanDisplay" t:object="object"/>
> </div>
>
> <div id="serviceRenderedComponent">
> <t:outputRaw value="serviceRenderedComponent"/>
>
>
>
> >
> > If anybody has any ideas that would be great.
> >
> > Cheers,
> >
> > Adriaan
> >
>
>
> --
> Thiago
>

Re: Rendering a tapestry component to a string

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, Oct 11, 2021 at 12:47 PM Adriaan Joubert <ad...@gmail.com>
wrote:

> Hi,
>

Hello, Adriaan!


> We are starting to use a library called fancytree (
> http://wwwendt.de/tech/fancytree/demo/index.html). For basic trees this
> integrates really easily with tapestry.
>
> However for tree tables the simplest way to render the trees client-side is
> to ship the contents of the tree table down in a json structure. For simple
> values this works well.
>
> Ideally we would like to have tapestry components available in the tree
> table. We could come up with complicated ways of doing this (hidden divs
> for every cell etc), but it would be easy if we could render a tapestry
> component to a string, and simply ship this client side as a field in the
> json.
>
> Does this sound feasible at all? I found
> https://github.com/uklance/tapestry-offline, but is there a simpler
> built-in way?
>

tapestry-offline is great when you want to use Tapestry to generate some
content outside of Tapestry-handled requests.

For Tapestry-handled requests, you can use its own PartialTemplateRenderer
service:
https://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/PartialTemplateRenderer.html.
It works for component and block instances.

One example taken from one of the Tapestry internal testing apps:

public class PartialTemplateRendererDemo
{

    @Inject
    @Property
    private Block someBlock;

    @Inject
    private PartialTemplateRenderer partialTemplateRenderer;

    @InjectComponent
    private BeanDisplay beanDisplay;

    @Property
    private Pojo object;

    void setupRender() {
        object = new Pojo();
        object.setDate(new Date(21342345234444L));
        object.setString(String.valueOf(System.currentTimeMillis()));
        object.setInteger((int) (System.currentTimeMillis() % 234123));
    }

    public String getServiceRenderedBlock() {
        return partialTemplateRenderer.render(someBlock);
    }

    public String getServiceRenderedComponent() {
        return partialTemplateRenderer.render(beanDisplay);
    }

    public static class Pojo
    {
        private int integer;
        private String string;
        private Date date;

        public int getInteger()
        {
            return integer;
        }

        public void setInteger(int i)
        {
            this.integer = i;
        }

        public String getString()
        {
            return string;
        }

        public void setString(String s)
        {
            this.string = s;
        }

        public Date getDate()
        {
            return date;
        }

        public void setDate(Date date)
        {
            this.date = date;
        }

    }
}

 <t:block id="someBlock">
<!-- One component that generates a good amount of markup -->
<t:beandisplay t:id="insideBlock" t:object="object"/>
</t:block>

<div id="original">
<t:delegate to="block:someblock"/>
</div>

<div id="serviceRenderedBlock">
<t:outputRaw value="serviceRenderedBlock"/>
</div>

<div id="originalBeanDisplay">
<t:beandisplay t:id="beanDisplay" t:object="object"/>
</div>

<div id="serviceRenderedComponent">
<t:outputRaw value="serviceRenderedComponent"/>



>
> If anybody has any ideas that would be great.
>
> Cheers,
>
> Adriaan
>


-- 
Thiago