calling template from the other template

2019-08-10 21:20发布

问题:

I am working on Catalyst MVC framework to create a web page for a database. I am not a full time programmer as such. But I know basics of perl and java. I am a biologist. I have written a template file(view.tt) which is having a table. I need to link the contents of this table to another table which is written in one more template file. I am not able to link these two templates(as I am not able to generate the url properly). Can any one help or guide me to resolve this issue.

The following code is for the second template file:

<h4>Analyses Table</h4>

<table id="analysis_table" width="90%">

<thead>

<tr>
  <th align="center" valign="center">Dataset</th>
  <th align="center" valign="center">Tissue</th>
  <th align="center" valign="center">Expression</th>
  <th align="center" valign="center">Analysis</th>
</tr>

</thead>
<tbody>
[% FOREACH result IN plets -%]
  [% IF result.analysistypename == 'Cancer' -%]

    [% matches = result.datasetname.match('(\w+) (\w+)') %]
    <tr>
    <td align="left" valign="top">[% result.datasetname %]</td>
    <td align="left" valign="top">[% result.expression %]</td>
    <td align="left" valign="top">[% result.datatype %]</td>
    <td align="left" valign="top">[% result.generankpercentile %]</td>
    </tr>
  [% END -%]
[% END -%]

</tbody>

<tfoot>

<tr>
  <th align="center" valign="center">Dataset</th>
  <th align="center" valign="center">Tissue</th>
  <th align="center" valign="center">Expression</th>
  <th align="center" valign="center">Analysis</th>
</tr>

</tfoot>

</table> 

The following piece of code is in my first template which has the table to be hyperlinked(view.tt)

<h4>Analyses Summary table</h4>

<table id="score_table" width="50%">

<thead>

<tr>
<th align="centre" valign="centre"> Analysis Type</th>
<th align="centre" valign="centre">Score</th>
</tr>

</thead>

<tbody>

[% FOREACH score IN gene.whs -%]

[% plots = [ ['Cancer', score.cancer_vs_normal_over, score.cancer_vs_normal_under],
['Bladder cancer', score.bladder_cancer_over, score.bladder_cancer_under],
['Brain and CNS cancer', score.brain_and_cns_cancer_over, score.brain_and_cns_cancer_under],
['Breast Cancer', score.breast_cancer_over, score.breast_cancer_under],
['Cervical Cancer', score.cervical_cancer_over, score.cervical_cancer_under],
['Colorectal cancer', score.colorectal_cancer_over, score.colorectal_cancer_under],
]
%]


[% FOREACH plot IN plots %]
<tr>
<td> [% pl0t.0 %]</td>
<td> [% pl0t.1 %] </td>
<td> [% pl0t.2 %] </td>
<td> [% (pl0t.1 + 5 ) / (plot.2 + 5) | format('%5.2f') %] </td>
</tr>
[% END %]

[% END %]

</tbody>

<tfoot>
<tr>
<th align="centre" valign="centre"> Analysis Type</th>
<th align="centre" valign="centre">Score</th>
</tr>
</tfoot>

</table>

Hi Ret, Thanks a lot and sorry for late reply. As you have understood my aim is to have a 'live' link between the summary table and a second table loaded with results. I have somehow linked the templates by another way.I had to hyperlink in first template to second template. My hyperlinking is in the foreach loop which looks something like this.

[% FOREACH plot IN plots %]
    <tr>
    <td><a href="[% c.uri_for(c.controller('gene').action_for('sample'))%]/[% plot.0 %]>[% plot.0 %]</a></td>
    <td> [% pl0t.1 %] </td>
    <td> [% pl0t.2 %] </td>
    <td> [% (pl0t.1 + 5 ) / (plot.2 + 5) | format('%5.2f') %] </td>
    </tr>
    [% END %]

This line is linking correctly to the second template(sample.tt). The second template contains a series of tables. when the hyperlink is clicked on the first table, the table corresponding to that link should be displayed. I have successfully linked the templates. I have created a sub routine in my controller(gene.pm) also for the purpose of linking the templates. I am facing a problem in achieving my goal. The html part inthe second template(sample.tt) is functioning properly,but the template toolkit is not functioning. I am able to see the change in the url but in the page it is not reflected.

Below is the subroutine for the second template:

sub sample :Path('sample'):Args(1) {
         my($self,$c,$plot)=@_;
         my @plets = $c->model('GeneDB::genewhs')->search({
         Analysistypename => @plet,
 });
         $c->stash->{plets}=\@plets;
         $c->stash->{template}='gene/sample.tt';

 }

If in case the question is not clear, please let me know...i would be obliged to clear it..

Thanks in advance..

回答1:

It is possible to simply PROCESS or INCLUDE another template in the one currently being processed, but this doesn't allow for user interaction. I take it your aim is to have a 'live' link between the summary table and a second table loaded with results following some selection from the first? It's not completely clear from your question.

But if I understand correctly, you will need to do one of (at least) two things:

  1. Load ALL the results into a series of tables, and add an ID and CSS class to each table to indicate its type. Use jQuery (or similar) to hide all and show the relevant table based on clicking in the summary table. This will work if the results volume is not that great, and means you don't need to come to grips with AJAX. (You could also put all the results in a single table and use a CSS class to identify type.)
  2. Write a Catalyst Action that receives whatever parameters are required to identify the results to be returned, and return them as a JSON array. The calling function is responsible for layout of the table. I wouldn't recommend this unless you were very comfortable with jQuery, AJAX and Javascript in general, or the volume of all results are prohibitive.

Having said #2, it looks like you've already got all the results stashed anyway. How they're linked is less clear. (I can't help thinking this data should be normalised on the Catalyst side, rather than decoded with the plots array on the template side.) So assuming you go with option #1, you'll need something like:

[%- 
    #add a 'key' field with no spaces suitable for jQuery as the last element
    plots = [
        ['Cancer', score.cancer_vs_normal_over, score.cancer_vs_normal_under, 'cancer'],
        ['Bladder cancer', score.bladder_cancer_over, score.bladder_cancer_under, 'bladder-cancer' ],
-%]

[% FOREACH plot IN plots %]
<tr>
<td class="linktoresults" rel="#[%plot.3%]"> [% plot.0 %]</td>
...
[% END %]

[%- FOREACH ... # It's just not clear how plots relate to gene.whs
    SET rel = ... # how this result links back to the plot array from earlier.
    PROCESS "secondarytable.tt";
    END;
-%]

<script src="jquery.js"></script>
<script>
    $(document).ready(function({
        $(".linktoresults").click(function({
             id = $(this).attr('rel');
             $(".analysis_table").hide(); // hide all tables
             $(id).show(); // show the relevant one for this item
        });
    });
</script>

And your secondarytable.tt template needs to have:

<table class="analysis_table" id="[%rel%]" width="90%">

...so the click function can link to the subordinate tables.

Hopefully these code snippets will give you a push in the right direction. The relationship between plots and gene.whs is not clear enough for me to give you much more, I'm afraid.