This file is indexed.

/usr/share/doc/python-myghty/html/modulecomponents.html is in python-myghty 1.1-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
<html>
<head>
	<title>
Myghty Documentation
</title>
	<link href="style.css" rel="stylesheet" type="text/css"></link>
	<link href="syntaxhighlight.css" rel="stylesheet" type="text/css"></link>
</head>
<body>




<link href="docs.css" rel="stylesheet" type="text/css"></link>


<div style="position:absolute;left:0px;top:0px;"><a name="top"></a>&nbsp;</div>

<div class="doccontainer">

<div class="docheader">

<div class="docheadertext" >Myghty Documentation</div>
<div class="">Version: 1.1   Last Updated: 09/10/06 19:32:29</div>
</div>



		<A name="modulecomponents"></a>
		
	
<div class="topnav">


	
	<div class="topnavcontrol">
	View: <b>Paged</b> &nbsp;|&nbsp; <a href="documentation.html">One Page</a>
	</div>



<div class="topnavsectionlink">

<a href="index.html">Table of Contents</a>

<div class="prevnext">
Previous: <a href="components.html">Components</a>

&nbsp; | &nbsp;


Next: <a href="request.html">The Request</a>

</div>
</div>


<div class="topnavmain">
	<div class="topnavheader">Module Components</div>
	<div class="topnavitems">
	


    
    
    <div class="toclink">
        <A style="" href="#modulecomponents_example">Example Module Component</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#modulecomponents_flavor">Flavors of Module Component</a>
    </div>
    
    <div class="toclinkcontainer">
        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_flavor_function">Function</a>
    </div>

    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_flavor_callable">Callable Object</a>
    </div>

    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_flavor_method">Object Method</a>
    </div>

    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_flavor_explicit">Class / Explicit Module Component</a>
    </div>

    </div>


    </div>


    
    
    <div class="toclink">
        <A style="" href="#modulecomponents_initialization">Module Component Initialization</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#modulecomponents_controller">Using MCs as Controllers</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#modulecomponents_resolution">Configuring Module Component Resolution</a>
    </div>
    
    <div class="toclinkcontainer">
        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_resolution_module_components">module_components</a>
    </div>

        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_resolution_module_components_additional">Passing Resolution Arguments</a>
    </div>

    </div>


    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_resolution_module_root">module_root</a>
    </div>

        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_resolution_module_root_options">Module Root Options</a>
    </div>

    </div>


    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_resolution_routesresolver">Routes Resolver</a>
    </div>

    </div>


    </div>


    
    
    <div class="toclink">
        <A style="" href="#modulecomponents_preprocessor">Module Component Pre and Post processing</a>
    </div>
    
    <div class="toclinkcontainer">
    </div>


    
    
    <div class="toclink">
        <A style="" href="#modulecomponents_templates">Using Module Components within Templates</a>
    </div>
    
    <div class="toclinkcontainer">
        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#modulecomponents_templates_formexample">Example: Form Visitor</a>
    </div>

    </div>


    </div>



	</div>
</div>

</div>

		<div class="sectioncontent">
			




<div class="subsection" style="margin-left:10px;">

    <div class="sectiontext">


<P>Module components build upon the component concept as introduced and explained in the previous section, <a href="components.html#components" >Components</a>.  They represent a way of writing Myghty components using regular Python code within importable .py modules.  A module component can be used to handle requests, controlling the flow of an application across one or more templates; this role is known as a "controller".  Module components can also be embedded as component calls within templates just like any other component.   In both cases, Module Components are the appropriate place to place code-intensive control or view logic.</p>

<p>Module Components, or MC's, are not a new idea, and have analogies in other environments, such as the Java Servlet class, the mod_python handler() function, or the WSGI application() callable.  To suit the preferences of the both the class-oriented and the function-oriented developer, MC's can be written either as classes that subclass a specific Myghty class (similar to subclassing the Java Servlet class) or just as a regular Python function or callable object (i.e., an object instance that has a __call__ method).  Additionally, individual methods upon a class instance can also be called as MC's.  </p>

<p>When a plain Python function, callable object, or arbitrary instance method (herein will be generically referred to as a "callable") is to be called as a Module Component, the resulting component is referred to as an <b>implicit</b> module component.  In this case, the Myghty Interpreter is given the location of the callable either via a string pattern or via passing the callable itself, and is told to execute it as a component.  The Interpreter then creates a "real" component behind the scenes which <i>proxies</i> the Myghty argument set to the callable.  The developer need not be aware of this proxying component (though it is available).
</p>

<p>The flipside to the implicit module component is the <b>explicit</b> module component.  Here, the developer creates a class that is a subclass of <span class="codeline">myghty.component.ModuleComponent</span>, and implements the method <span class="codeline">do_run_component()</span>, which satisfies the execution of the component.  Like the implicit module component, the Interpreter is given its location either via a string pattern or via the Class object itself, and is told to execute it.  The Interpreter, upon receiving a Class object, instantiates the class which becomes directly the component to be executed; no "proxy" component is created.
</p>

<p>The differences between implicit and explicit module components are mostly differences in style.  The explicit form allows the developer more direct access to the flags, attributes, and argument lists of the component to be called, whereas the implicit form is more sparse, lightweight and flexible.  Both styles can be mixed; essentially, the Intepreter receives the object and determines the calling style based on the type of object passed to it.   In all cases, the rest of a Myghty application sees just more <span class="codeline">myghty.component.Component</span> objects, which have the same properties as any other component.
</p>

<p>An added layer of complexity to module components involves the configuration of URI resolution, which allows the mapping of arbitrary URI's to specific module component callables or classes based on different schemes.  This is required for MC's that are to be called as the top-level, request-handling component.  Myghty currently includes two methods of achieving this, which are described later in this section, as well as an API to allow any user-defined mapping algorithm (part of <a href="resolver.html#resolver" >Advanced Resolver Configuration</a>).</p>



			

			



			



			


			



			


			


    </div>

        



<A name="modulecomponents_example"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Example Module Component</span>
    <div class="sectiontext">


<p>Lets start with hello world.  All we need to do is create a new .py file, called <span class="codeline">hello.py</span>, and place the following function inside it:</p>



<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">helloworld</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world !"</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p>That is the extent of the module component itself.  Since it is a plain function, this is an "implicit" module component.</p>

<p>To call the component, the request object is given a string starting with the prefix "MODULE:", followed by the module name and name of the callable or class, separated by a colon.  In a template it is one of the following:</p>



<div class="code">
<pre><span class="doctag"># inline
</span><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:hello:helloworld </span><span class="compcall">&amp;&gt;</span><span class="text">

</span><span class="doctag"># code
</span><span class="blocktag">&lt;%python&gt;</span>
<span class="python_operator">    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">comp</span><span class="python_enclosure">(</span><span class="python_literal">"MODULE:hello:helloworld"</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span></pre></div>


<p>Alternatively (release 0.99b), the "MODULE:" prefix can also be an "at" sign as follows:</p>


<div class="code">
<pre><span class="compcall">&lt;&amp; </span><span class="compname">@hello:helloworld </span><span class="compcall">&amp;&gt;</span><span class="text">

</span><span class="blocktag">&lt;%python&gt;</span>
<span class="python_operator">    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">comp</span><span class="python_enclosure">(</span><span class="python_literal">"@hello:helloworld"</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span></pre></div>

<p>
Both the module name and the callable/class name can contain any number of periods to represent sub-modules or sub-properties of the module.  Suppose <span class="codeline">helloworld</span> was a property on an object:
</p>



<div class="code">
<pre><span class="python_name">hello </span><span class="python_operator">= </span><span class="python_name">object</span><span class="python_enclosure">()</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">doit</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world !"</span><span class="python_enclosure">)</span><span class="python_operator">
    </span>
<span class="python_operator"></span><span class="python_name">hello</span><span class="python_operator">.</span><span class="python_name">helloworld </span><span class="python_operator">= </span><span class="python_name">doit</span><span class="python_operator"></span></pre></div>


<p>This component can be called as:</p>


<div class="code">
<pre><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:hello:hello.helloworld </span><span class="compcall">&amp;&gt;</span></pre></div>

<p>or just:</p>


<div class="code">
<pre><span class="compcall">&lt;&amp; </span><span class="compname">@hello:hello.helloworld </span><span class="compcall">&amp;&gt;</span></pre></div>



<p>The callable can have any argument list, and even the <i>m</i> variable is optional (though highly recommended).  Myghty uses the <span class="codeline">inspect</span> library to determine the desired arguments based on the callable's signature.  (for the performance minded, this is done only once when the component is first loaded).  Using the names present in the argument list, Myghty will determine which of the available global variables will be specified, as well as what required and optional component arguments this MC will require.  The component arguments are configured in exactly the same way as a template-based component configures its &lt;%ARGS&gt; section.</p>

<P>So to our "hello world" lets add the current and optionally tomorrow's weather:</p>



<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">helloworld</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_operator">, </span><span class="python_name">today</span><span class="python_operator">, </span><span class="python_name">tomorrow </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world !  its a %s day today." </span><span class="python_operator">% </span><span class="python_name">today</span><span class="python_enclosure">)</span><span class="python_operator">
    </span><span class="python_keyword">if </span><span class="python_name">tomorrow </span><span class="python_keyword">is </span><span class="python_name">None</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"Don't know what it is tomorrow, though."</span><span class="python_enclosure">)</span><span class="python_operator">
    </span><span class="python_keyword">else</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"But tomorrow, it'll be %s" </span><span class="python_operator">% </span><span class="python_name">tomorrow</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p>This component can be called like these examples:</p>



<div class="code">
<pre><span class="doctag"># inline shorthand
</span><span class="compcall">&lt;&amp; </span><span class="compname">@hello:helloworld</span><span class="python_operator">, </span><span class="python_name">today</span><span class="python_operator">=</span><span class="python_literal">'sunny'</span><span class="python_operator"> </span><span class="compcall">&amp;&gt;</span><span class="text">

</span><span class="doctag"># inline longhand
</span><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:hello:helloworld</span><span class="python_operator">, </span><span class="python_name">today</span><span class="python_operator">=</span><span class="python_literal">'sunny'</span><span class="python_operator"> </span><span class="compcall">&amp;&gt;</span><span class="text">

</span><span class="doctag"># code
</span><span class="blocktag">&lt;%python&gt;</span>
<span class="python_operator">    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">comp</span><span class="python_enclosure">(</span><span class="python_literal">"MODULE:hello:helloworld"</span><span class="python_operator">, </span><span class="python_name">today</span><span class="python_operator">=</span><span class="python_literal">'cloudy'</span><span class="python_operator">, </span><span class="python_name">tomorrow</span><span class="python_operator">=</span><span class="python_literal">'rainy'</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span></pre></div>



<p>The argument list of a module component may specify any of the standard and/or user-configured global arguments (globals were introduced in <a href="globals.html#globals" >Standard Global Variables</a>).  All arguments are passed by name, so the ordering is not important:
</p>



<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">handle_request</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_operator">, </span><span class="python_name">ARGS</span><span class="python_operator">, </span><span class="python_name">r</span><span class="python_operator">, </span><span class="python_name">s</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_comment"># ...</span><span class="python_operator"></span></pre></div>

<p>The above component specifies that it will receive the global variables <i>m</i>, <i>ARGS</i>, <i>r</i>, and <i>s</i>.  It also defines <span class="codeline">**params</span>, so all remaining component arguments will be in this dictionary.  A callable that defines simply <span class="codeline">**params</span> will receive <i>all</i> globals and component arguments within the dictionary.</p>

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>


        



<A name="modulecomponents_flavor"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Flavors of Module Component</span>
    <div class="sectiontext">


<p>A summary of the various styles of MC are as follows.  For each of the below examples, assume the callable exists within a module named <span class="codeline">mylib.hello</span>:</p>


			


			


			


			



    </div>

        



<A name="modulecomponents_flavor_function"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Function</span>
    <div class="sectiontext">


    This is the most basic type of implicit module component.
    

<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">helloworld</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world!"</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>

    
    Called as:
    
    

<div class="code">
<pre><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:mylib.hello:helloworld </span><span class="compcall">&amp;&gt;</span></pre></div>

    

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>


        



<A name="modulecomponents_flavor_callable"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Callable Object</span>
    <div class="sectiontext">


    <p>A callable object must be instantiated, but to the outside world looks almost the same as a function:</p>
    

<div class="code">
<pre><span class="python_keyword">class </span><span class="python_name">HelloWorld</span><span class="python_operator">:
    </span><span class="python_keyword">def </span><span class="python_name">__call__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world!"</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
<span class="python_comment"># instantiate the class
</span><span class="python_operator"></span><span class="python_name">helloworld </span><span class="python_operator">= </span><span class="python_name">HelloWorld</span><span class="python_enclosure">()</span><span class="python_operator"></span></pre></div>

    
    Called as:
    
    

<div class="code">
<pre><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:mylib.hello:helloworld </span><span class="compcall">&amp;&gt;</span></pre></div>

    
    Note that this is identical to the previous function example.
    

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>


        



<A name="modulecomponents_flavor_method"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Object Method</span>
    <div class="sectiontext">


    The object method is set up similarly to a callable object, the only difference being that a specific named method is called, instead of the generic <span class="codeline">__call__</span> method.  The difference lies in how it is specified when called.
    

<div class="code">
<pre><span class="python_keyword">class </span><span class="python_name">HelloWorld</span><span class="python_operator">:
    </span><span class="python_keyword">def </span><span class="python_name">doit</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world!"</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
<span class="python_comment"># instantiate the class
</span><span class="python_operator"></span><span class="python_name">helloworld </span><span class="python_operator">= </span><span class="python_name">HelloWorld</span><span class="python_enclosure">()</span><span class="python_operator"></span></pre></div>

    
    Called as:
    
    

<div class="code">
<pre><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:mylib.hello:helloworld.doit </span><span class="compcall">&amp;&gt;</span></pre></div>

    
    One particular quirk about the object method style is that if an object instance contains multiple methods that are called as MC's, the interpreter creates a <i>separate</i> "proxy" component for each method.</p>
    
    <p>With both callable object styles, the <i>same</i> object instance handles <i>many</i> requests, including multiple simultaneous requests among different threads, if the application is running in a threaded environment. A developer should take care to store request-scoped information as attributes upon the request object and <i>not</i> as instance variables of the object, as well as to appropriately synchronize access to instance variables and module attributes.

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>


        



<A name="modulecomponents_flavor_explicit"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Class / Explicit Module Component</span>
    <div class="sectiontext">


    <p>This style works differently from the previously mentioned styles, in that the developer is writing a class directly against the Myghty API.  In this style, the developer directly defines the class of the component that is ultimately being accessed by the outside world.  No "proxying" component is created; instead, the Interpreter instantiates the specified class the first time it is referenced.  This also removes the need to explicitly instantiate the class:
    
    

<div class="code">
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">component</span><span class="python_operator">
</span>
<span class="python_keyword">class </span><span class="python_name">HelloWorld</span><span class="python_enclosure">(</span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">component</span><span class="python_operator">.</span><span class="python_name">ModuleComponent</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span>
<span class="python_operator">    </span><span class="python_keyword">def </span><span class="python_name">do_run_component</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world!"</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>

    
    Called as:
    
    

<div class="code">
<pre><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:mylib.hello:HelloWorld </span><span class="compcall">&amp;&gt;</span></pre></div>

    
    <p>Note that the scoping rules for this style of component are the same, i.e. the same component instance is called for many requests and must be aware of variable scope as well as concurrency issues.</p>
    
    <p>The <span class="codeline">do_run_component</span> method works similarly to the callable of an implicit component.  Its argument list is dynamically inspected to determine the desired globals and component arguments, in the same way as that of implicit MC's.  However, there also is the option to bypass this inspection process and name the arguments explicitly, as well as their desired scope.  In previous versions of Myghty, this was the only way to define the arguments of an MC, but now its only optional.  This method is described in the next section, as well as the configuration of flags and attributes for MC's.

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>





</div>


        



<A name="modulecomponents_initialization"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Module Component Initialization</span>
    <div class="sectiontext">



<p>Explicit MCs as well as implicit MCs based on an object instance have an optional initialization step that is called the first time the object instance is referenced. The method is called on an explicit ModuleComponent subclass as:
</p>
<span class="code">def do_component_init(self)</span>
<p>
And is called on an implicit callable object as:
</p>
<span class="code">def do_component_init(self, component)</span>

<p>In the second version, the argument <span class="codeline">component</span> is an instance of <span class="codeline">myghty.component.FunctionComponent</span> that is <b>hosting</b> the implicit module component's <span class="codeline">__call__</span> method or other method.   It is important to note that an object instance with many callable methods will actually have many FunctionComponents created, each one hosting an individual methods...<b>however</b>, the do_component_init() method is only called <b>once</b> with whatever FunctionComponent was first associated with the object.</p>

<p> In previous versions of Myghty, the initialization phase was required for components with arguments, which had to be explicitly declared within this stage.  As of version 0.98, explicit declaration of component arguments is optional, and the argument lists are by default determined from the signature of the explicit <span class="codeline">do_run_component</span> method or the implicit callable.</p>

<p>Explicit specification of an MCs arguments in the init section are as follows:</p>



<div class="code">
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">component </span><span class="python_keyword">as </span><span class="python_name">component</span><span class="python_operator">
</span>
<span class="python_keyword">class </span><span class="python_name">MyComponent</span><span class="python_enclosure">(</span><span class="python_name">component</span><span class="python_operator">.</span><span class="python_name">ModuleComponent</span><span class="python_enclosure">)</span><span class="python_operator">:
</span>
<span class="python_operator">    </span><span class="python_keyword">def </span><span class="python_name">do_component_init</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_comment"># component arguments
</span><span class="python_operator">        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">args </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'arg1'</span><span class="python_operator">, </span><span class="python_literal">'arg2'</span><span class="python_enclosure">]</span><span class="python_operator">
        </span>
        <span class="python_comment"># required component arguments
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">required_args </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'arg3'</span><span class="python_operator">, </span><span class="python_literal">'arg4'</span><span class="python_enclosure">]</span><span class="python_operator">
        </span>
        <span class="python_comment"># request-only arguments
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">request_args </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'docid'</span><span class="python_enclosure">]</span><span class="python_operator">
        </span>
        <span class="python_comment"># required request-only arguments
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">required_request_args </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'userid'</span><span class="python_enclosure">]</span><span class="python_operator">
        </span>
        <span class="python_comment"># subrequest or request arguments
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">subrequest_args </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'foo'</span><span class="python_enclosure">]</span><span class="python_operator">
        </span>
        <span class="python_comment"># required subrequest or request arguments
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">required_subrequest_args </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'bar'</span><span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>

<p>
A component as above would have the <span class="codeline">do_run_component</span> signature as follows:
</p>


<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">do_run_component</span><span class="python_enclosure">(</span>
    <span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, </span><span class="python_name">userid</span><span class="python_operator">, </span><span class="python_name">arg3</span><span class="python_operator">, </span><span class="python_name">arg4</span><span class="python_operator">, </span><span class="python_name">bar</span><span class="python_operator">, </span>
    <span class="python_name">docid </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_operator">, </span><span class="python_name">arg1 </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_operator">, </span><span class="python_name">arg2 </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_operator">, </span><span class="python_name">foo </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_operator">, **</span><span class="python_name">params</span>
    <span class="python_enclosure">)</span><span class="python_operator">:</span></pre></div>

<p>Note that if a module component defines any arguments explicitly in the do_component_init method, all arguments for the do_run_component method must be specified; no automatic introspection of function arguments will occur.</p>

<p>Similarly, <%flags> and <%attr> sections can be achieved like this:
</p>


<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">do_component_init</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_comment"># flags
</span><span class="python_operator">    </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">flags </span><span class="python_operator">= </span><span class="python_enclosure">{</span>
        <span class="python_literal">'autoflush'</span><span class="python_operator">:</span><span class="python_name">True</span><span class="python_operator">,</span>
        <span class="python_literal">'trim'</span><span class="python_operator">: </span><span class="python_literal">'both'</span>
    <span class="python_enclosure">}</span><span class="python_operator">
    </span>
    <span class="python_comment"># attributes
    </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">attr </span><span class="python_operator">= </span><span class="python_enclosure">{</span>
        <span class="python_literal">'style'</span><span class="python_operator">:</span><span class="python_literal">'green'</span><span class="python_operator">,</span>
        <span class="python_literal">'docid'</span><span class="python_operator">:</span><span class="python_number">5843</span>
    <span class="python_enclosure">}</span><span class="python_operator"></span></pre></div>


    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>


        



<A name="modulecomponents_controller"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Using MCs as Controllers</span>
    <div class="sectiontext">


<p>The two most prominent features of an MC used as a controller includes that URI resolution is configured so that one or more URIs result in the component being called directly from a request, and that the component typically uses subrequests to forward execution onto a template, which serves as the view.</p>

<p>Here are two versions of a controller component that is used to pull documents from a database based on the request arguments, and displays them via a template called "documents.myt".
</p>
<p><b>Figure 1: Implicit Style, using a Callable Object</b></p>


<div class="code">
<pre> 
<span class="python_operator"> </span><span class="python_keyword">class </span><span class="python_name">DocumentManager</span><span class="python_operator">:
</span>
<span class="python_operator">     </span><span class="python_keyword">def </span><span class="python_name">do_component_init</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">component</span><span class="python_enclosure">)</span><span class="python_operator">:
         </span><span class="python_comment"># load some application-wide constants via Interpreter attributes.
         # the Interpreter is located via the hosting component.
</span><span class="python_operator">         </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">document_base </span><span class="python_operator">= </span><span class="python_name">component</span><span class="python_operator">.</span><span class="python_name">interpreter</span><span class="python_operator">.</span><span class="python_name">attributes</span><span class="python_enclosure">[</span><span class="python_literal">'document_base'</span><span class="python_enclosure">]</span><span class="python_operator">
         </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">db_string </span><span class="python_operator">= </span><span class="python_name">component</span><span class="python_operator">.</span><span class="python_name">interpreter</span><span class="python_operator">.</span><span class="python_name">attributes</span><span class="python_enclosure">[</span><span class="python_literal">'db_connect_string'</span><span class="python_enclosure">]</span><span class="python_operator">
</span>
     <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">__call__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, </span><span class="python_name">docid </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_enclosure">)</span><span class="python_operator">:
</span>
         <span class="python_comment"># if no document id, return '404 - not found'       
</span><span class="python_operator">         </span><span class="python_keyword">if </span><span class="python_name">docid </span><span class="python_keyword">is </span><span class="python_name">None</span><span class="python_operator">:
             </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">abort</span><span class="python_enclosure">(</span><span class="python_number">404</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
         <span class="python_comment"># access a hypothetical document database
         </span><span class="python_operator"></span><span class="python_name">docdb </span><span class="python_operator">= </span><span class="python_name">get_doc_db</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">document_base</span><span class="python_operator">, </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">db_string</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
         <span class="python_comment"># load document
         </span><span class="python_name">document </span><span class="python_operator">= </span><span class="python_name">docdb</span><span class="python_operator">.</span><span class="python_name">find_document</span><span class="python_enclosure">(</span><span class="python_name">docid</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
         <span class="python_comment"># couldnt find document - return '404 - not found'
         </span><span class="python_keyword">if </span><span class="python_name">document </span><span class="python_keyword">is </span><span class="python_name">None</span><span class="python_operator">:
             </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">abort</span><span class="python_enclosure">(</span><span class="python_number">404</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
         <span class="python_comment"># run template
         </span><span class="python_operator"></span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">subexec</span><span class="python_enclosure">(</span><span class="python_literal">'documents.myt'</span><span class="python_operator">, </span><span class="python_name">document </span><span class="python_operator">= </span><span class="python_name">document</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
 <span class="python_operator"></span><span class="python_name">documentmanager </span><span class="python_operator">= </span><span class="python_name">DocumentManager</span><span class="python_enclosure">()</span><span class="python_operator"></span></pre></div>



<p><b>Figure 2: Explicit Style, with Pre-Declared Arguments</b></p>


<div class="code">
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">component </span><span class="python_keyword">as </span><span class="python_name">component</span><span class="python_operator">
</span>
<span class="python_keyword">class </span><span class="python_name">DocumentManager</span><span class="python_enclosure">(</span><span class="python_name">component</span><span class="python_operator">.</span><span class="python_name">ModuleComponent</span><span class="python_enclosure">)</span><span class="python_operator">:
</span>
<span class="python_operator">    </span><span class="python_keyword">def </span><span class="python_name">do_component_init</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_comment"># load some application-wide constants
</span><span class="python_operator">        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">document_base </span><span class="python_operator">= </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">interpreter</span><span class="python_operator">.</span><span class="python_name">attributes</span><span class="python_enclosure">[</span><span class="python_literal">'document_base'</span><span class="python_enclosure">]</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">db_string </span><span class="python_operator">= </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">interpreter</span><span class="python_operator">.</span><span class="python_name">attributes</span><span class="python_enclosure">[</span><span class="python_literal">'db_connect_string'</span><span class="python_enclosure">]</span><span class="python_operator">
</span>
        <span class="python_comment"># establish the argument names we want (optional as of 0.98)
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">args </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'docid'</span><span class="python_enclosure">]</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">do_run_component</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, </span><span class="python_name">ARGS</span><span class="python_operator">, </span><span class="python_name">docid </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
</span>
        <span class="python_comment"># if no document id, return '404 - not found'       
</span><span class="python_operator">        </span><span class="python_keyword">if </span><span class="python_name">docid </span><span class="python_keyword">is </span><span class="python_name">None</span><span class="python_operator">:
            </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">abort</span><span class="python_enclosure">(</span><span class="python_number">404</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
        <span class="python_comment"># access a hypothetical document database
        </span><span class="python_operator"></span><span class="python_name">docdb </span><span class="python_operator">= </span><span class="python_name">get_doc_db</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">document_base</span><span class="python_operator">, </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">db_string</span><span class="python_enclosure">)</span><span class="python_operator">
        </span>
        <span class="python_comment"># load document
        </span><span class="python_name">document </span><span class="python_operator">= </span><span class="python_name">docdb</span><span class="python_operator">.</span><span class="python_name">find_document</span><span class="python_enclosure">(</span><span class="python_name">docid</span><span class="python_enclosure">)</span><span class="python_operator">
        </span>
        <span class="python_comment"># couldnt find document - return '404 - not found'
        </span><span class="python_keyword">if </span><span class="python_name">document </span><span class="python_keyword">is </span><span class="python_name">None</span><span class="python_operator">:
            </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">abort</span><span class="python_enclosure">(</span><span class="python_number">404</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
        <span class="python_comment"># run template
        </span><span class="python_operator"></span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">subexec</span><span class="python_enclosure">(</span><span class="python_literal">'documents.myt'</span><span class="python_operator">, </span><span class="python_name">document </span><span class="python_operator">= </span><span class="python_name">document</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>

<p>
The next section describes the two methods of configuring request URIs to execute "controller" components like this one.
</p>

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>


        



<A name="modulecomponents_resolution"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Configuring Module Component Resolution</span>
    <div class="sectiontext">


<p>
Module components can be used anywhere within the request, either right at the beginning (i.e. a controller) or within the scope of other components already executing.   A module component can be fetched and/or called based on its importable module name followed by a path to a callable object, or the name of a class that subclasses <span class="codeline">myghty.components.ModuleComponent</span>.  However, this doesn't work as a URL sent to a webserver. For request-level operation, MCs must be mapped to URIs.  This resolution is achieved through two different configuration directives, <span class="codeline">module_components</span> and <span class="codeline">module_root</span>.  A third option also exists which is to use the new <span class="codeline">routesresolver</span> resolver object.</p>


			


			

			


    </div>

        



<A name="modulecomponents_resolution_module_components"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">module_components</span>
    <div class="sectiontext">


The module_components configuration parameter looks like this:



<div class="code">
<pre><span class="python_name">module_components </span><span class="python_operator">= </span><span class="python_enclosure">[</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'myapplication/home/.*' </span><span class="python_operator">: </span><span class="python_literal">'myapp.home:HomeHandler'</span><span class="python_enclosure">}</span><span class="python_operator">, </span>
    <span class="python_enclosure">{</span><span class="python_literal">r'myapplication/login/.*' </span><span class="python_operator">: </span><span class="python_literal">'myapp.login:LoginHandler'</span><span class="python_enclosure">}</span><span class="python_operator">, </span>
    <span class="python_enclosure">{</span><span class="python_literal">r'.*/cart' </span><span class="python_operator">: </span><span class="python_literal">'myapp.cart:process_cart'</span><span class="python_enclosure">}</span><span class="python_operator">, </span>
    <span class="python_enclosure">{</span><span class="python_literal">r'.*' </span><span class="python_operator">: </span><span class="python_literal">'myapp.home:HomeHandler'</span><span class="python_enclosure">} </span>
<span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>


Which in an apache configuration looks like:



<div class="code">
<pre>PythonOption MyghtyModuleComponents "[ \
    {r'myapplication/home/.*' : 'myapp.home:HomeHandler'}, \
    {r'myapplication/login/.*' : 'myapp.login:LoginHandler'}, \
    {r'.*/cart' : 'myapp.cart:ShoppingCart'}, \
    {r'.*' : 'myapp.home:HomeHandler'} \
]"</pre></div>


<p>
Each entry in the module_components array is a single-member hash containing a regular expression to be matched against the incoming URI, and an expression that can be resolved into a Module Component.  This expression can be any of the following forms:
</p>


<div class="code">
<pre><span class="python_name">module_components </span><span class="python_operator">= </span><span class="python_enclosure">[</span>
    <span class="python_comment"># a string in the form '&lt;modulename&gt;:&lt;classname&gt;'           </span>
    <span class="python_enclosure">{</span><span class="python_literal">r'myapplication/home/.*' </span><span class="python_operator">: </span><span class="python_literal">'myapp.home:HomeHandler'</span><span class="python_enclosure">}</span><span class="python_operator">, </span>
    
    <span class="python_comment"># a string in the form '&lt;modulename&gt;:&lt;functionname&gt;'</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'myapplication/login/.*' </span><span class="python_operator">: </span><span class="python_literal">'myapp.login:do_login'</span><span class="python_enclosure">}</span><span class="python_operator">, </span>
    
    <span class="python_comment"># a string in the form '&lt;modulename&gt;:&lt;some&gt;.&lt;callable&gt;.&lt;thing&gt;'</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'myapplication/status/.*' </span><span class="python_operator">: </span><span class="python_literal">'myapp.status:handler.mcomp.get_status'</span><span class="python_enclosure">}</span><span class="python_operator">,</span>
    
    <span class="python_comment"># a Class</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'.*/cart' </span><span class="python_operator">: </span><span class="python_name">MyClass</span><span class="python_enclosure">}</span><span class="python_operator">, </span>
    
    <span class="python_comment"># a function reference, or reference to a callable() object</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'.*/dostuff' </span><span class="python_operator">: </span><span class="python_name">do_stuff</span><span class="python_enclosure">}</span><span class="python_operator">,</span>
    
    <span class="python_comment"># reference to a class instance method</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'.*' </span><span class="python_operator">: </span><span class="python_name">mymodule</span><span class="python_operator">.</span><span class="python_name">processor</span><span class="python_operator">.</span><span class="python_name">main_process</span><span class="python_enclosure">}</span>
<span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>

<p>
Generally, its better to use the string forms since they encompass all the information needed to load the class or callable object, which allows the Myghty interpreter to dynamically reload the object when the underlying module has changed.
</p>

<p>For module components that resolve to a Class, the Class will be instantiated by the Interpreter, and is expected to be a subclass of <span class="codeline">myghty.component.ModuleComponent</span>.  This is also known as an <b>explicit</b> module component.  For components that resolve to a function, object instance method, or callable object (i.e. any object that has a __call__ method), a "wrapper" module component will be created automatically which adapts to the callable's argument list.  This is also known as an <b>implicit</b> module component.
</p>


			

<p>The corresponding Resolver class for module_components is ResolveModule.
</p>


    </div>

        



<A name="modulecomponents_resolution_module_components_additional"></a>

<div class="subsection" style="margin-left:40px;">

    <span class="sectionheadertext">Passing Resolution Arguments</span>
    <div class="sectiontext">



<p>The module_components configuration parameter also includes some methods of returning information about the actual resolution of the component at request time.</p>

<p>Since module_components uses regular expressions to match URIs to components, the <span class="codeline">re.Match</span> object produced when a match occurs can be accessed via the <i>m</i> global variable.  This example places capturing parenthesis in the regular expression to capture the additional path information:</p>



<div class="code">
<pre><span class="python_name">module_components </span><span class="python_operator">= </span><span class="python_enclosure">[</span>
    <span class="python_comment"># place capturing parenthesis in the regexp</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'/catalog/(.*)' </span><span class="python_operator">: </span><span class="python_literal">'store:Catalog'</span><span class="python_enclosure">}</span>
<span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>


The contents of the capturing parenthesis are available as:



<div class="code">
<pre><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">resolution</span><span class="python_operator">.</span><span class="python_name">match</span><span class="python_operator">.</span><span class="python_name">group</span><span class="python_enclosure">(</span><span class="python_number">1</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p>User-defined arguments can also be configured which will be passed to the request object at resolution time.  To use this form, a dictionary is used instead of a single string to specify the module and callable/class name of the component, and the address of the component itself is placed under the key 'component':</p>



<div class="code">
<pre><span class="python_name">module_components </span><span class="python_operator">= </span><span class="python_enclosure">[</span>
    <span class="python_comment"># supply the component with some arguments</span>
    <span class="python_enclosure">{</span><span class="python_literal">r'/login/' </span><span class="python_operator">: </span>
        <span class="python_enclosure">{</span>
        <span class="python_literal">'component' </span><span class="python_operator">: </span><span class="python_literal">'myapp:loginmanager'</span><span class="python_operator">,</span>
        <span class="python_literal">'ldap_server' </span><span class="python_operator">: </span><span class="python_literal">'ldap://localhost:5678'</span>
        <span class="python_enclosure">}</span>
    <span class="python_enclosure">}</span>
<span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>


The contents of the <span class="codeline">ldap_server</span> argument are available as:



<div class="code">
<pre><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">resolution</span><span class="python_operator">.</span><span class="python_name">args</span><span class="python_enclosure">[</span><span class="python_literal">'ldap_server'</span><span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>


    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>





</div>


        



<A name="modulecomponents_resolution_module_root"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">module_root</span>
    <div class="sectiontext">


<p>This parameter locates module components based on paths.  It is similar to the <a href="params.html#parameters_component_root" >component_root</a> configuration parameter in that it defines one or more roots which will all be matched against the incoming URI.  However, it not only traverses through  directories, it also will traverse into a Python module where it attempts to locate a function or callable object instance.  Currently, only the "implicit" style of module components can be used with module_root (but module_components, from the previous section, can be used with implicit or explicit MCs).
</p>

<p>The entries in module_root take the following forms:</p>



<div class="code">
<pre><span class="python_name">module_root </span><span class="python_operator">= </span><span class="python_enclosure">[</span>
    <span class="python_comment"># a regular file path</span>
    <span class="python_literal">'/web/lib/modules/'</span><span class="python_operator">,</span>
    
    <span class="python_comment"># the full file path of a Python module</span>
    <span class="python_literal">'/web/extra/modules/mycontroller.py'</span><span class="python_operator">,</span>
    
    <span class="python_comment"># the name of an importable Python module</span>
    <span class="python_literal">'mylib.loginhandlers'</span><span class="python_operator">,</span>
<span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>


<p>Using the first path '/web/lib/modules', heres an example.  Start with the following file:</p>



<div class="code">
<pre>/web/lib/modules/admin/login.py</pre></div>


<p>This file contains the following code:</p>



<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">hello</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"hello world!"</span><span class="python_enclosure">)</span><span class="python_operator">
    </span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">_do_login</span><span class="python_operator">:
    </span><span class="python_keyword">def </span><span class="python_name">__call__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"please login"</span><span class="python_enclosure">)</span><span class="python_operator">
        </span><span class="python_comment"># ....
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">foo</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_literal">"this is foo"</span><span class="python_enclosure">)</span><span class="python_operator">
        </span>
<span class="python_operator"></span><span class="python_name">index </span><span class="python_operator">= </span><span class="python_name">_do_login</span><span class="python_enclosure">()</span><span class="python_operator">
</span>
<span class="python_name">_my_var </span><span class="python_operator">= </span><span class="python_number">12</span><span class="python_operator"></span></pre></div>


<p>Finally, lets have a module_root of:</p>



<div class="code">
<pre><span class="python_name">module_root </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'/web/lib/modules'</span><span class="python_enclosure">]</span><span class="python_operator"></span></pre></div>


<p>With this configuration, URLs will be resolved as follows:</p>



<div class="code">
<pre>http://mysite.com/admin/login/hello/     ---&gt;  "hello world!"    (login.py:hello())
http://mysite.com/admin/login/           ---&gt;  "please login"    (login.py:index.__call__())
http://mysite.com/admin/login/foo/       ---&gt;  "this is foo"     (login.py:index.foo())
http://mysite.com/admin/login/lala/      ---&gt;  "please login"    (login.py:index.__call__())
http://mysite.com/admin/login/_my_var/   ---&gt;  "please login"    (_my_var is private and is skipped)
http://mysite.com/admin/lala/            ---&gt;  404 not found     (no file named lala.py)</pre></div>

<p>The spirit of this resolver is that of the mod_python Publisher handler.   Path tokens are converted into the names of .py files on the filesystem.  When a file is located and it is a valid Python module, the module is loaded and further path tokens are resolved as attributes within the module itself.  Attributes that start with an underscore are skipped, which is the default way to flag "private" attributes.  There is also a "public" attribute marker which can be used to mark public functions instead of marking private functions, explained in the options section below.  If no matching attribute exists for a path token, it looks for the attribute name "index", else returns a ComponentNotFound exception.
</p>

			
<p>module_root can also be combined with dhandler resolution (described in <a href="specialtempl.html#specialtempl_dhandler" >dhandler</a>), so that a file named <span class="codeline">dhandler.py</span> in the current or parent directory serves as the "default" Python module for a path token that does not correspond to any other file.  In the case of ResolvePathModule locating a dhandler.py file, the additional path tokens that normally reside within <a href="request.html#request_members_dhandler_path" >dhandler_path</a> are also searched as attributes within the dhandler.py module.
</p>

<p>The corresponding Resolver class for module_root is ResolvePathModule.
</p>

    </div>

        



<A name="modulecomponents_resolution_module_root_options"></a>

<div class="subsection" style="margin-left:40px;">

    <span class="sectionheadertext">Module Root Options</span>
    <div class="sectiontext">


<p>The underlying Resolver object for the module_root parameter is called ResolvePathModule (resolver objects are described in <a href="resolver.html#resolver" >Advanced Resolver Configuration</a>).  This object also has configuration options of its own.  These options may be specified to the ResolvePathModule constructor directly.  They can be specified as regular Myghty configuration parameters as well as of version 0.99b:
    <ul>
        <li><p>module_root_adjust=None (release 0.99b) - a reference to a function which receives the incoming URI and returns a modified version of that URI with which to resolve.  The modified URI is not propigated onto other rules in the resolution chain, so therefore it represents a "local" alternative to <a href="params.html#parameters_path_translate" >path_translate</a>.</p>
        <p>This parameter is also called 'adjust' in all releases, but that name conflicts with the same name in the ResolveFile resolver, so should only be used when constructing individual ResolvePathModule objects and not as a global configuration parameter.</p>
        </li>
        <li><p>require_publish=False - when set to True, the callable object must have an attribute <span class="codeline">publish</span> set to True in order to mark it as publically accessible; else it will not be resolved.  This effectively reverses the "negative publishing" model established via the underscore "_" syntax into a "positive publishing" model.  Given this configuration:</p>
        
        

<div class="code">
<pre><span class="python_name">module_root </span><span class="python_operator">= </span><span class="python_enclosure">[</span><span class="python_literal">'/web/lib/modules'</span><span class="python_enclosure">]</span><span class="python_operator">
</span><span class="python_name">require_publish </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_operator"></span></pre></div>

        
<p>Marking functions and methods public looks like this:
    

<div class="code">
<pre><span class="python_keyword">class </span><span class="python_name">MyController</span><span class="python_enclosure">(</span><span class="python_name">object</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_keyword">def </span><span class="python_name">__call__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">kwargs</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_comment"># ...
</span><span class="python_operator">        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">subexec</span><span class="python_enclosure">(</span><span class="python_literal">'mytemplate.myt'</span><span class="python_enclosure">)</span><span class="python_operator">
    </span><span class="python_name">__call__</span><span class="python_operator">.</span><span class="python_name">public </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_operator">
        </span>
    <span class="python_keyword">def </span><span class="python_name">do_stuff</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">kwargs</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_comment"># ...
</span><span class="python_operator">        </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">subexec</span><span class="python_enclosure">(</span><span class="python_literal">'dostuff.myt'</span><span class="python_enclosure">)</span><span class="python_operator">
    </span><span class="python_name">do_stuff</span><span class="python_operator">.</span><span class="python_name">public </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_operator">
    </span>
<span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">call_index</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_operator">,</span><span class="python_name">r</span><span class="python_operator">, </span><span class="python_name">s</span><span class="python_operator">, </span><span class="python_name">ARGS</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_comment"># ...
</span><span class="python_operator">    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">subexec</span><span class="python_enclosure">(</span><span class="python_literal">'index.myt'</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="python_name">call_index</span><span class="python_operator">.</span><span class="python_name">public </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_operator">
</span>
<span class="python_name">mycallable </span><span class="python_operator">= </span><span class="python_name">MyController</span><span class="python_enclosure">()</span><span class="python_operator"></span></pre></div>
    
    
<p>The public attribute can also be set in Python 2.4 via decorator:</p>
    

<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">mark_public</span><span class="python_enclosure">(</span><span class="python_name">func</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">func</span><span class="python_operator">.</span><span class="python_name">public </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_operator">
    </span><span class="python_keyword">return </span><span class="python_name">func</span><span class="python_operator">
    </span>
<span class="python_operator">@</span><span class="python_name">mark_public</span><span class="python_operator">
</span><span class="python_keyword">def </span><span class="python_name">mycontroller</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">kwargs</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_comment"># ...</span><span class="python_operator"></span></pre></div>

        </li>
        <li>path_moduletokens=['index'] - this is a list of special "default" token names when searching for module attributes.  For example:  If a given path "/foo/bar/bat" resolves to the module "bar.py" inside of directory "foo/", it will first look for an attribute named "bat" inside the module.  If "bat" is not present, it will then look for all the names inside the moduletokens list, which defaults to the single entry "index".  Therefore /foo/bar/bat not only looks for "bat", it also looks for "index".  The tokens inside moduletokens are checked as defaults for every module token, so that a path such as "/foo/bar/bat" can also correspond to a module attribute "foo.index.bat", "index.bar.bat", etc.</li>
        <li>path_stringtokens=[] - this is the path equivalent to the moduletokens parameter.  It is a list of "default" path tokens when searching for files.  In some ways, this parameter's functionality overlaps that of <a href="params.html#parameters_dhandler_name" >dhandler_name</a>, except it allows multiple names and applies only to the module_root configuration.  For example, if stringtokens is configured to ["default", "index"], and a given path "/foo/bar" resolves to the directory "foo/" but there is no file named "bar.py" inside the directory, it will then look for the files "default.py" and "index.py", respectively.  Like moduletokens, stringtokens also checks these defaults for every path token, so a URL of "/foo/bar/bat" can also match "/default/bar/index", for example.</li>
    </ul>

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>





</div>


        



<A name="modulecomponents_resolution_routesresolver"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Routes Resolver</span>
    <div class="sectiontext">


<P>The Routes Resolver provides Rails-like functionality, building upon the resolution architecture described in <a href="resolver.html#resolver" >Advanced Resolver Configuration</a>.  Since it is not part of the default set of resolver objects, it must be included in the resolution chain via the <a href="params.html#parameters_resolver_strategy" >resolver_strategy</a> configuration parameter.</p>

<p>A rudimentary example of the Routes resolver can be viewed by installing the <span class="codeline">myghty_routes</span> Paste template.  Instructions for installing Paste templates are in <a href="installation.html#installation_paste" >Running Paste Templates</a>.  The routes resolver also serves as the core resolver for the <a href="http://pylons.groovie.org">Pylons</a> web framework which is based on Myghty, and includes more comprehensive and up-to-date examples.
</p>

    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>





</div>


        



<A name="modulecomponents_preprocessor"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Module Component Pre and Post processing</span>
    <div class="sectiontext">



<p>Request-handling module components can also be used to perform pre- and post-processing on a request.  This means that the information about a request is modified before and/or after the main response is determined.  The following two examples both wrap the main body of the request inside of a subrequest.</p>

<p>A module component that performs translations on incoming URI's, and then passes control onto the requested template, looks like this:</p>



<div class="code">
<pre><span class="python_keyword">def </span><span class="python_name">translate_path</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">path </span><span class="python_operator">= </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">get_request_path</span><span class="python_enclosure">()</span><span class="python_operator">
    </span><span class="python_name">path </span><span class="python_operator">= </span><span class="python_name">re</span><span class="python_operator">.</span><span class="python_name">sub</span><span class="python_enclosure">(</span><span class="python_literal">'/sitedocs/john/'</span><span class="python_operator">, </span><span class="python_literal">'/'</span><span class="python_operator">, </span><span class="python_name">path</span><span class="python_enclosure">)</span><span class="python_operator">
    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">subexec</span><span class="python_enclosure">(</span><span class="python_name">path</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>

<p>A URI configuration for this component might look like:</p>



<div class="code">
<pre><span class="python_name">module_components </span><span class="python_operator">= </span><span class="python_enclosure">[{</span><span class="python_literal">r'/sitedocs/john/.*\.myt'</span><span class="python_operator">, </span><span class="python_literal">'mymodule:translate_path'</span><span class="python_enclosure">}]</span><span class="python_operator"></span></pre></div>


<p>Path translation is also accomplished in Myghty via the <a href="params.html#parameters_path_translate" >path_translate</a> parameter, and can be controlled in finer detail through the use of custom resolution rules, described in <a href="resolver.html#resolver" >Advanced Resolver Configuration</a>.</p>

<p>An example post processing component, which filters the output of the subrequest before returning data:
</p>



<div class="code">
<pre><span class="python_keyword">import </span><span class="python_name">StringIO</span><span class="python_operator">, </span><span class="python_name">re</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">filter</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_comment"># make a buffer
</span><span class="python_operator">    </span><span class="python_name">buf </span><span class="python_operator">= </span><span class="python_name">StringIO</span><span class="python_operator">.</span><span class="python_name">StringIO</span><span class="python_enclosure">()</span><span class="python_operator">
</span>
    <span class="python_comment"># create a subrequest using that buffer
    # our templates are located relative to the 
    # request URI underneath '/components'
    </span><span class="python_name">subreq </span><span class="python_operator">= </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">create_subrequest</span><span class="python_enclosure">(</span><span class="python_literal">'/components' </span><span class="python_operator">+ </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">get_request_uri</span><span class="python_enclosure">()</span><span class="python_operator">, </span><span class="python_name">out_buffer </span><span class="python_operator">= </span><span class="python_name">buf</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
    <span class="python_comment"># execute the subrequest
    </span><span class="python_name">ret </span><span class="python_operator">= </span><span class="python_name">subreq</span><span class="python_operator">.</span><span class="python_name">execute</span><span class="python_enclosure">()</span><span class="python_operator">
</span>
    <span class="python_comment"># perform a meaningless filter operation on the content
    </span><span class="python_name">content </span><span class="python_operator">= </span><span class="python_name">re</span><span class="python_operator">.</span><span class="python_name">sub</span><span class="python_enclosure">(</span><span class="python_literal">r'foo'</span><span class="python_operator">, </span><span class="python_literal">r'bar'</span><span class="python_operator">, </span><span class="python_name">buf</span><span class="python_operator">.</span><span class="python_name">getvalue</span><span class="python_enclosure">())            </span><span class="python_operator">
</span>
    <span class="python_comment"># write the content                 
    </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">write</span><span class="python_enclosure">(</span><span class="python_name">content</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>

<p>This component might be configured as:</p>



<div class="code">
<pre><span class="python_name">module_components </span><span class="python_operator">= </span><span class="python_enclosure">[{</span><span class="python_literal">r'/sitedocs/john/.*\.myt'</span><span class="python_operator">, </span><span class="python_literal">'mymodule:filter'</span><span class="python_enclosure">}]</span><span class="python_operator"></span></pre></div>


<p>Filtering is also built in to Myghty via the use of <%filter> section, described in <a href="filtering.html#filtering" >Filtering and Flushing (and Escaping)</a>.
</p>


    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>


        



<A name="modulecomponents_templates"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Using Module Components within Templates</span>
    <div class="sectiontext">


<p>MCs can also be called from within templates.  If an MC is configured to be callable against a URI using <span class="codeline">module_components</span> or <span class="codeline">module_root</span>, that URI can also be used within a regular &lt;&amp; &amp;&gt;-style component call.  Or, the MODULE: prefix style described previously can be used.</p>


			




    </div>

        



<A name="modulecomponents_templates_formexample"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Example: Form Visitor</span>
    <div class="sectiontext">


<p>Here is an example of a module component that is used to generate HTML forms from a data object, using the visitor pattern.  Such an architecture can be used to have form information stored in a database or XML file which can then be used to automatically construct a corresponding form.  This example is included in working form with the Myghty distribution.
</p>

<p><b>Step 1: Data Model</b> - The data model consists of a hierarchy of <span class="codeline">FormElement</span> objects.  Some <span class="codeline">FormElement</span> objects can contain collections of other <span class="codeline">FormElements</span> (the "composite" pattern):</p>


<div class="code">
    <div class="codetitle">form.py</div>
<pre><span class="python_keyword">class </span><span class="python_name">FormElement</span><span class="python_enclosure">(</span><span class="python_name">object</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_literal">""" abstract FormElement superclass."""</span><span class="python_operator">
    </span>
    <span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_keyword">pass</span><span class="python_operator">
    </span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">visitor</span><span class="python_enclosure">)</span><span class="python_operator">: 
        </span><span class="python_literal">"""subclasses override accept_visitor to provide
        the appropriate type-based visitor method."""</span><span class="python_operator">
        </span><span class="python_keyword">pass</span><span class="python_operator">
    </span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">Form</span><span class="python_enclosure">(</span><span class="python_name">FormElement</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_literal">"""represents a &lt;FORM&gt; tag."""</span><span class="python_operator">
</span>
    <span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">name</span><span class="python_operator">, </span><span class="python_name">action</span><span class="python_operator">, </span><span class="python_name">elements</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">name </span><span class="python_operator">= </span><span class="python_name">name</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">action </span><span class="python_operator">= </span><span class="python_name">action</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">elements </span><span class="python_operator">= </span><span class="python_name">elements</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">visitor</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">visitor</span><span class="python_operator">.</span><span class="python_name">visit_form</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator">
    </span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">Field</span><span class="python_enclosure">(</span><span class="python_name">FormElement</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_literal">"""abstract base class for individual user-entry fields, each 
    having a name, a description, and an optional value."""</span><span class="python_operator">
</span>
    <span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">name</span><span class="python_operator">, </span><span class="python_name">description</span><span class="python_operator">, </span><span class="python_name">value </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">name </span><span class="python_operator">= </span><span class="python_name">name</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">description </span><span class="python_operator">= </span><span class="python_name">description</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">value </span><span class="python_operator">= </span><span class="python_name">value</span><span class="python_operator">
    </span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">TextField</span><span class="python_enclosure">(</span><span class="python_name">Field</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_literal">"""an &lt;INPUT TYPE="text"&gt; field."""</span><span class="python_operator">
</span>
    <span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">name</span><span class="python_operator">, </span><span class="python_name">description</span><span class="python_operator">, </span><span class="python_name">size</span><span class="python_operator">, </span><span class="python_name">value </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">Field</span><span class="python_operator">.</span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">name</span><span class="python_operator">, </span><span class="python_name">description</span><span class="python_operator">, </span><span class="python_name">value</span><span class="python_enclosure">)</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">size </span><span class="python_operator">= </span><span class="python_name">size</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">visitor</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">visitor</span><span class="python_operator">.</span><span class="python_name">visit_textfield</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator">
        </span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">SelectField</span><span class="python_enclosure">(</span><span class="python_name">Field</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_literal">"""a &lt;SELECT&gt;&lt;/SELECT&gt; tag."""</span><span class="python_operator">
</span>
    <span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">name</span><span class="python_operator">, </span><span class="python_name">description</span><span class="python_operator">, </span><span class="python_name">options</span><span class="python_operator">, </span><span class="python_name">value </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">Field</span><span class="python_operator">.</span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">name</span><span class="python_operator">, </span><span class="python_name">description</span><span class="python_operator">, </span><span class="python_name">value</span><span class="python_enclosure">)</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">options </span><span class="python_operator">= </span><span class="python_name">options</span><span class="python_operator">
        </span><span class="python_keyword">for </span><span class="python_name">o </span><span class="python_keyword">in </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">options</span><span class="python_operator">:
            </span><span class="python_name">o</span><span class="python_operator">.</span><span class="python_name">parent </span><span class="python_operator">= </span><span class="python_name">self</span><span class="python_operator">
            </span><span class="python_keyword">if </span><span class="python_name">o</span><span class="python_operator">.</span><span class="python_name">id </span><span class="python_operator">== </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">value</span><span class="python_operator">:
                </span><span class="python_name">o</span><span class="python_operator">.</span><span class="python_name">selected </span><span class="python_operator">= </span><span class="python_name">True</span><span class="python_operator">
                </span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">visitor</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">visitor</span><span class="python_operator">.</span><span class="python_name">visit_selectfield</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">OptionField</span><span class="python_enclosure">(</span><span class="python_name">FormElement</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_literal">"""an &lt;OPTION&gt;&lt;/OPTION&gt; tag.
    contains a parent attribute that points to a &lt;SELECT&gt; field."""</span><span class="python_operator">
</span>
    <span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">id</span><span class="python_operator">, </span><span class="python_name">description</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">id </span><span class="python_operator">= </span><span class="python_name">id</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">description </span><span class="python_operator">= </span><span class="python_name">description</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">parent </span><span class="python_operator">= </span><span class="python_name">None</span><span class="python_operator">
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">selected </span><span class="python_operator">= </span><span class="python_name">False</span><span class="python_operator">
        </span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">visitor</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">visitor</span><span class="python_operator">.</span><span class="python_name">visit_option</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">SubmitField</span><span class="python_enclosure">(</span><span class="python_name">FormElement</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_literal">"""an &lt;INPUT TYPE="submit"&gt; field."""</span><span class="python_operator">
</span>
    <span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">value</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">value </span><span class="python_operator">= </span><span class="python_name">value</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">visitor</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">visitor</span><span class="python_operator">.</span><span class="python_name">visit_submit</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p><b>Step 2: Visitor Interface</b> - This class provides the layout of a visitor object:</p>


<div class="code">
    <div class="codetitle">form.py</div>
<pre><span class="python_keyword">class </span><span class="python_name">FormVisitor</span><span class="python_operator">:
    </span><span class="python_keyword">def </span><span class="python_name">visit_form</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">form</span><span class="python_enclosure">)</span><span class="python_operator">:</span><span class="python_keyword">pass</span><span class="python_operator">
    </span><span class="python_keyword">def </span><span class="python_name">visit_textfield</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">textfield</span><span class="python_enclosure">)</span><span class="python_operator">:</span><span class="python_keyword">pass</span><span class="python_operator">
    </span><span class="python_keyword">def </span><span class="python_name">visit_selectfield</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">selectfield</span><span class="python_enclosure">)</span><span class="python_operator">:</span><span class="python_keyword">pass</span><span class="python_operator">
    </span><span class="python_keyword">def </span><span class="python_name">visit_option</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">option</span><span class="python_enclosure">)</span><span class="python_operator">:</span><span class="python_keyword">pass</span><span class="python_operator">
    </span><span class="python_keyword">def </span><span class="python_name">visit_submit</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">submit</span><span class="python_enclosure">)</span><span class="python_operator">:</span><span class="python_keyword">pass</span><span class="python_operator"></span></pre></div>


<p><b>Step 3: HTML Rendering Components</b> - a set of Myghty methods that will render the HTML of each form element:
</p>


<div class="code">
    <div class="codetitle">formfields.myc</div>
<pre><span class="doctag">&lt;%doc&gt;</span><span class="doctag_text">
    each method renders one particular HTML form element, 
    as well as &lt;table&gt; tags to provide basic layout for each.  
    the "form" and "select" methods are component calls with content, 
    to render the form elements within them.
</span><span class="doctag">&lt;/%doc&gt;</span><span class="text">

</span><span class="deftag">&lt;%method</span><span class="compname"> form</span><span class="deftag">&gt;</span><span class="text">
    </span><span class="argstag">&lt;%args&gt;</span><span class="python_name">form</span><span class="python_operator"></span><span class="argstag">&lt;/%args&gt;</span><span class="text">
    
    &lt;FORM name="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">name</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">" method="POST" action="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">action</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">"&gt;
    &lt;table&gt;
    </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">content</span><span class="python_enclosure">()</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">
    &lt;/table&gt;
    &lt;/FORM&gt;
</span><span class="deftag">&lt;/%method&gt;</span><span class="text">

</span><span class="deftag">&lt;%method</span><span class="compname"> textfield</span><span class="deftag">&gt;</span><span class="text">
    </span><span class="argstag">&lt;%args&gt;</span><span class="python_name">textfield</span><span class="python_operator"></span><span class="argstag">&lt;/%args&gt;</span><span class="text">

    &lt;tr&gt;
        &lt;td&gt;</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">textfield</span><span class="python_operator">.</span><span class="python_name">description</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">:&lt;/td&gt;
    
        &lt;td&gt;
        &lt;input type="text" name="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">textfield</span><span class="python_operator">.</span><span class="python_name">name</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">" value="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">textfield</span><span class="python_operator">.</span><span class="python_name">value</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">" size="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">textfield</span><span class="python_operator">.</span><span class="python_name">size</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">" &gt;
        &lt;/td&gt;
    &lt;/tr&gt;
</span><span class="deftag">&lt;/%method&gt;</span><span class="text">

</span><span class="deftag">&lt;%method</span><span class="compname"> submit</span><span class="deftag">&gt;</span><span class="text">
    </span><span class="argstag">&lt;%args&gt;</span><span class="python_name">submit</span><span class="python_operator"></span><span class="argstag">&lt;/%args&gt;</span><span class="text">
    
    &lt;tr&gt;&lt;td colspan="2"&gt;
        &lt;input type="submit" value="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">submit</span><span class="python_operator">.</span><span class="python_name">value</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">"&gt;
    &lt;/td&gt;&lt;/tr&gt;
</span><span class="deftag">&lt;/%method&gt;</span><span class="text">

</span><span class="deftag">&lt;%method</span><span class="compname"> select</span><span class="deftag">&gt;</span><span class="text">
    </span><span class="argstag">&lt;%args&gt;</span><span class="python_name">select</span><span class="python_operator"></span><span class="argstag">&lt;/%args&gt;</span><span class="text">
    
    &lt;tr&gt;
        &lt;td&gt;</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">select</span><span class="python_operator">.</span><span class="python_name">description</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">&lt;/td&gt;
        &lt;td&gt;
            &lt;select name="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">select</span><span class="python_operator">.</span><span class="python_name">name</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">"&gt;
            </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">content</span><span class="python_enclosure">()</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">
            &lt;/select&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
</span><span class="deftag">&lt;/%method&gt;</span><span class="text">

</span><span class="deftag">&lt;%method</span><span class="compname"> option</span><span class="deftag">&gt;</span><span class="text">
    </span><span class="argstag">&lt;%args&gt;</span><span class="python_name">option</span><span class="python_operator"></span><span class="argstag">&lt;/%args&gt;</span><span class="text">
    &lt;option value="</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">option</span><span class="python_operator">.</span><span class="python_name">id</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">" </span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">option</span><span class="python_operator">.</span><span class="python_name">selected </span><span class="python_keyword">and </span><span class="python_literal">"SELECTED" </span><span class="python_keyword">or </span><span class="python_literal">""</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">&gt;</span><span class="substitution">&lt;%</span><span class="python_operator"> </span><span class="python_name">option</span><span class="python_operator">.</span><span class="python_name">description</span><span class="python_operator"> </span><span class="substitution">%&gt;</span><span class="text">&lt;/option&gt;
</span><span class="deftag">&lt;/%method&gt;</span></pre></div>


<p><b>Step 4: Module Component</b> - ties it all together.  The FormGenerator serves as the traversal unit for the Form object, as well as the visitor implementation itself.  The visit_form and visit_selectfield methods both involve contained formelements, and the corresponding myghty components are component calls with content; so for those, a content function is created to handle the embedded content.
</p>


<div class="code">
    <div class="codetitle">form.py</div>
<pre><span class="python_keyword">def </span><span class="python_name">renderform</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_operator">, </span><span class="python_name">form</span><span class="python_operator">, **</span><span class="python_name">params</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">FormGenerator</span><span class="python_operator">.</span><span class="python_name">RenderForm</span><span class="python_enclosure">(</span><span class="python_name">m</span><span class="python_enclosure">))</span><span class="python_operator">
    </span>
<span class="python_operator"></span><span class="python_keyword">class </span><span class="python_name">RenderForm</span><span class="python_enclosure">(</span><span class="python_name">FormVisitor</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_keyword">def </span><span class="python_name">__init__</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">m</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">m </span><span class="python_operator">= </span><span class="python_name">m</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">visit_form</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">form</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_keyword">def </span><span class="python_name">formcontent</span><span class="python_enclosure">()</span><span class="python_operator">:
            </span><span class="python_keyword">for </span><span class="python_name">element </span><span class="python_keyword">in </span><span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">elements</span><span class="python_operator">:
                </span><span class="python_name">element</span><span class="python_operator">.</span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
        <span class="python_operator"></span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">execute_component</span><span class="python_enclosure">(</span><span class="python_literal">"formfields.myc:form"</span><span class="python_operator">, </span><span class="python_name">args </span><span class="python_operator">= </span><span class="python_name">dict</span><span class="python_enclosure">(</span><span class="python_name">form </span><span class="python_operator">= </span><span class="python_name">form</span><span class="python_enclosure">)</span><span class="python_operator">, </span><span class="python_name">content</span><span class="python_operator">=</span><span class="python_name">formcontent</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">visit_textfield</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">textfield</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">comp</span><span class="python_enclosure">(</span><span class="python_literal">"formfields.myc:textfield"</span><span class="python_operator">, </span><span class="python_name">textfield </span><span class="python_operator">= </span><span class="python_name">textfield</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">visit_selectfield</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">selectfield</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_keyword">def </span><span class="python_name">selectcontent</span><span class="python_enclosure">()</span><span class="python_operator">:
            </span><span class="python_keyword">for </span><span class="python_name">element </span><span class="python_keyword">in </span><span class="python_name">selectfield</span><span class="python_operator">.</span><span class="python_name">options</span><span class="python_operator">:
                </span><span class="python_name">element</span><span class="python_operator">.</span><span class="python_name">accept_visitor</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
        <span class="python_operator"></span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">execute_component</span><span class="python_enclosure">(</span><span class="python_literal">"formfields.myc:select"</span><span class="python_operator">, </span><span class="python_name">args </span><span class="python_operator">= </span><span class="python_name">dict</span><span class="python_enclosure">(</span><span class="python_name">select </span><span class="python_operator">= </span><span class="python_name">selectfield</span><span class="python_enclosure">)</span><span class="python_operator">, </span><span class="python_name">content</span><span class="python_operator">=</span><span class="python_name">selectcontent</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">visit_option</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">option</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">comp</span><span class="python_enclosure">(</span><span class="python_literal">"formfields.myc:option"</span><span class="python_operator">, </span><span class="python_name">option </span><span class="python_operator">= </span><span class="python_name">option</span><span class="python_enclosure">)</span><span class="python_operator">
</span>
    <span class="python_operator"></span><span class="python_keyword">def </span><span class="python_name">visit_submit</span><span class="python_enclosure">(</span><span class="python_name">self</span><span class="python_operator">, </span><span class="python_name">submit</span><span class="python_enclosure">)</span><span class="python_operator">:
        </span><span class="python_name">self</span><span class="python_operator">.</span><span class="python_name">m</span><span class="python_operator">.</span><span class="python_name">comp</span><span class="python_enclosure">(</span><span class="python_literal">"formfields.myc:submit"</span><span class="python_operator">, </span><span class="python_name">submit </span><span class="python_operator">= </span><span class="python_name">submit</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p><b>Step 5: Fire it Up</b> - this myghty template actually calls the <span class="codeline">renderform</span> Module Component.
</p>


<div class="code">
    <div class="codetitle">registration.myt</div>
<pre><span class="doctag">&lt;%doc&gt;</span><span class="doctag_text">
    an example firstname/lastname/occupation registration form.
</span><span class="doctag">&lt;/%doc&gt;</span><span class="text">

</span><span class="blocktag">&lt;%python&gt;</span>
<span class="python_operator">    </span><span class="python_keyword">import </span><span class="python_name">form</span><span class="python_operator">
    </span><span class="python_name">registerform </span><span class="python_operator">= </span><span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">Form</span><span class="python_enclosure">(</span><span class="python_literal">'myform'</span><span class="python_operator">, </span><span class="python_literal">'register.myt'</span><span class="python_operator">, </span><span class="python_name">elements </span><span class="python_operator">= </span><span class="python_enclosure">[</span>
            <span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">TextField</span><span class="python_enclosure">(</span><span class="python_literal">'firstname'</span><span class="python_operator">, </span><span class="python_literal">'First Name'</span><span class="python_operator">, </span><span class="python_name">size</span><span class="python_operator">=</span><span class="python_number">50</span><span class="python_enclosure">)</span><span class="python_operator">,</span>
            <span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">TextField</span><span class="python_enclosure">(</span><span class="python_literal">'lastname'</span><span class="python_operator">, </span><span class="python_literal">'Last Name'</span><span class="python_operator">, </span><span class="python_name">size</span><span class="python_operator">=</span><span class="python_number">50</span><span class="python_enclosure">)</span><span class="python_operator">,</span>
            <span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">SelectField</span><span class="python_enclosure">(</span><span class="python_literal">'occupation'</span><span class="python_operator">, </span><span class="python_literal">'Occupation'</span><span class="python_operator">, </span>
                <span class="python_name">options </span><span class="python_operator">= </span><span class="python_enclosure">[</span>
                    <span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">OptionField</span><span class="python_enclosure">(</span><span class="python_literal">'skydiver'</span><span class="python_operator">, </span><span class="python_literal">'Sky Diver'</span><span class="python_enclosure">)</span><span class="python_operator">,</span>
                    <span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">OptionField</span><span class="python_enclosure">(</span><span class="python_literal">'programmer'</span><span class="python_operator">, </span><span class="python_literal">'Computer Programmer'</span><span class="python_enclosure">)</span><span class="python_operator">,</span>
                    <span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">OptionField</span><span class="python_enclosure">(</span><span class="python_literal">''</span><span class="python_operator">, </span><span class="python_literal">'No Answer'</span><span class="python_enclosure">)</span><span class="python_operator">,</span>
                <span class="python_enclosure">]</span>
            <span class="python_enclosure">)</span><span class="python_operator">,</span>
            <span class="python_name">form</span><span class="python_operator">.</span><span class="python_name">SubmitField</span><span class="python_enclosure">(</span><span class="python_literal">'register'</span><span class="python_enclosure">)</span>
        <span class="python_enclosure">]</span>
    <span class="python_enclosure">)</span><span class="python_operator">
</span><span class="blocktag">&lt;/%python&gt;</span><span class="text">

Welcome to the Skydivers or Computer Programmers Club !

</span><span class="compcall">&lt;&amp; </span><span class="compname">MODULE:form:renderform</span><span class="python_operator">, </span><span class="python_name">form </span><span class="python_operator">= </span><span class="python_name">registerform</span><span class="python_operator"> </span><span class="compcall">&amp;&gt;</span></pre></div>



    </div>


    <a href="#modulecomponents" class="toclink">back to section top</a>


</div>





</div>




    
<div class="sectionnavblock">
<div class="sectionnav">


        Previous: <a href="components.html">Components</a>

                |

        Next: <a href="request.html">The Request</a>

</div>
</div>


</div>


		</div>






</div>

















<center>
</center>
</body>
</html>