upvar and uplevel - help

Liste des GroupesRevenir à cl tcl 
Sujet : upvar and uplevel - help
De : user2276 (at) *nospam* newsgrouper.org.invalid (sundar)
Groupes : comp.lang.tcl
Date : 16. Mar 2025, 05:37:56
Autres entêtes
Message-ID : <1742099876-2276@newsgrouper.org>
User-Agent : Newsgrouper/0.7.0

```
proc render {data css template} {
    # Generate scoped CSS and get container class
    lassign [scope_css $css] scoped_css container_class
   
    # Create a container with the scoped CSS
    set result [div [list class $container_class] \
            [style {} $scoped_css] \
            [eval $template]]
    return $result
}
proc mytest::card_page {} {
    variable card_styles
    # Create some sample cards using your card proc
    set cards_data [list \
[dict create \
     title "Performance" \
     body "Track your metrics..." \
     footer [a {href "#"} "View Details"]] \
[dict create \
     title "Revenue" \
     body "Monitor revenue..." \
     footer [a {href "#"} "View Report"]]
   ]
    set template {
        div {class "card-grid"} \
        {*}[lmap card $data {
                dict with card {}
                article {class "card"} \
                    [h3 {class "card-title"} $title] \
                    [div {class "card-content"} $body] \
                    [expr {$footer ne "" ?
               [div {class "card-footer"} $footer] : ""}]
            }]
    }
    return [render $cards_data $card_styles $template]
}
```
Hello Tclers,
In the above code I want to use 'upvar' and 'uplevel' in the 'template' so as
to use the variables declared in the surrounding scope (cards_data & card_styles, in this case),
instead of passing it as arguments to the 'render' function.
One way is,
```
set template {
        upvar cards_data data
        div {class "card-grid"} \
.....
}
```
But is there a way to make the 'upvar' use any variable (table_data, list_data)
where we don't have to make the required changes at each place where the 'templeat'
is needed?
Any other suggestion to refactor is also most welcome.

Date Sujet#  Auteur
16 Mar 25 * upvar and uplevel - help4sundar
16 Mar 25 `* Re: upvar and uplevel - help3Arjen
17 Mar 25  +- Re: upvar and uplevel - help1sundar
19 Mar 25  `- Re: upvar and uplevel - help1sundar

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal