/usr/share/perl5/HTML/Template/Compiled.pm is in libhtml-template-compiled-perl 1.003-1.
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 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 | package HTML::Template::Compiled;
our $VERSION = '1.003'; # VERSION
use Data::Dumper;
use Scalar::Util;
BEGIN {
use constant D => $ENV{HTC_DEBUG} || 0;
}
use strict;
use warnings;
use Digest::MD5 qw/ md5_hex /;
use Carp;
use Fcntl qw(:seek :flock);
use File::Spec;
use File::Basename qw(dirname basename);
use HTML::Template::Compiled::Utils qw(:walkpath :log :escape &md5);
use HTML::Template::Compiled::Expression qw(:expressions);
use HTML::Template::Compiled::Compiler;
# TODO
eval {
require URI::Escape;
};
#eval {
# require Encode;
#};
#my $Encode = $@ ? 0 : 1;
use base 'Exporter';
our @EXPORT_OK = qw(&HTC);
use HTML::Template::Compiled::Parser qw(
$CASE_SENSITIVE_DEFAULT $NEW_CHECK
$DEBUG_DEFAULT $SEARCHPATH
%FILESTACK %COMPILE_STACK %PATHS $DEFAULT_ESCAPE $DEFAULT_QUERY
$UNTAINT $DEFAULT_TAGSTYLE $MAX_RECURSE
);
use vars qw($__ix__);
use constant MTIME => 0;
use constant CHECKED => 1;
use constant LMTIME => 2;
use constant LCHECKED => 3;
use constant DEBUG_COMPILED => 0b001;
use constant DEBUG_CACHE_FILE_MISS => 0b0001;
use constant DEBUG_CACHE_FILE_HIT => 0b0010;
use constant DEBUG_CACHE_MEM_MISS => 0b0100;
use constant DEBUG_CACHE_MEM_HIT => 0b1000;
our $DEBUG = 0;
our $LAST_EXCEPTION;
# options / object attributes
use constant PARAM => 0;
BEGIN {
my @map = (
undef, qw(
path md5_path filename file scalar filehandle
file_cache cache_dir cache search_path
loop_context case_sensitive global_vars
default_path
debug debug_file objects perl out_fh default_escape
filter formatter
globalstack use_query parse_tree parser compiler includes
plugins open_mode chomp expire_time strict warnings line_info
args optimize
)
#use_expressions
);
for my $i ( 1 .. $#map ) {
my $method = "_$map[$i]";
my $get = sub { return $_[0]->[$i] };
my $set;
$set = sub { $_[0]->[$i] = $_[1] };
no strict 'refs';
*{"get$method"} = $get;
*{"set$method"} = $set;
}
}
# tired of typing?
sub HTC { __PACKAGE__->new(@_) }
sub new {
my ( $class, %args ) = @_;
D && $class->log("new()");
# handle the "type", "source" parameter format (does anyone use it?)
if ( exists $args{type} ) {
exists $args{source} or $class->_error_no_source();
$args{type} =~ m/^(?:filename|scalarref|arrayref|filehandle)$/
or $class->_error_wrong_source();
$args{ $args{type} } = $args{source};
delete $args{type};
delete $args{source};
}
if (exists $args{filename}) {
return $class->new_file($args{filename}, %args);
}
elsif (exists $args{scalarref}) {
return $class->new_scalar_ref($args{scalarref}, %args);
}
elsif (exists $args{filehandle}) {
return $class->new_filehandle($args{filehandle}, %args);
}
elsif (exists $args{arrayref}) {
return $class->new_array_ref($args{arrayref}, %args);
}
croak("$class->new called with not enough arguments");
}
sub _error_no_query {
my ($self) = @_;
my $class = ref $self || $self;
carp "You are using query() but have not specified that you want to use it"
. " (specify with use_query => 1)";
}
sub _error_not_compiled {
my ($self) = @_;
my $class = ref $self || $self;
carp "Template was not compiled yet";
}
sub _error_wrong_source {
my ($self) = @_;
my $class = ref $self || $self;
croak("$class->new() : type parameter must be set to 'filename', "
. "'arrayref', 'scalarref' or 'filehandle'!");
}
sub _error_no_source {
my ($self) = @_;
my $class = ref $self || $self;
croak("$class->new() called with 'type' parameter set,"
. " but no 'source'!");
}
sub _error_template_sources {
my ($self) = @_;
my $class = ref $self || $self;
croak(
"$class->new called with multiple (or no) template sources specified!"
. "A valid call to new() has exactly ne filename => 'file' OR exactly one"
. " scalarref => \\\$scalar OR exactly one arrayref => \\\@array OR"
. " exactly one filehandle => \*FH"
);
}
sub _error_empty_filename {
my ($self) = @_;
my $class = ref $self || $self;
croak("$class->new called with empty filename parameter!");
}
sub new_from_perl {
my ($class, %args) = @_;
my $self = bless [], $class;
$self->init_args(\%args);
D && $self->log("new(perl) filename: $args{filename}");
$self->init_cache(\%args);
$self->init(%args);
$self->set_perl( $args{perl} );
$self->set_filename( $args{filename} );
my $md5path = md5_hex(@{ $args{path} || [] });
$self->set_path( $args{path} );
$self->set_md5_path( $md5path );
$self->set_scalar( $args{scalarref} );
unless ( $self->get_scalar ) {
my $file =
$self->createFilename( $self->get_path, \$self->get_filename );
$self->set_file($file);
}
return $self;
}
sub new_file {
my ($class, $filename, %args) = @_;
my $self = bless [], $class;
$self->init_args(\%args);
$args{path} = $self->build_path($args{path});
$self->_error_empty_filename()
if (!defined $filename or !length $filename);
$args{filename} = $filename;
if (exists $args{scalarref}
|| exists $args{arrayref} || exists $args{filehandle}) {
$self->_error_template_sources;
}
$self->set_filename( $filename );
$self->init_cache(\%args);
my $md5path = md5_hex(@{ $args{path} || [] });
$self->set_path( $args{path} );
$self->set_md5_path( $md5path );
if (my $t = $self->from_cache(\%args)) {
$t->init_includes();
return $t;
}
$self->init(%args);
$self->from_scratch;
$self->init_includes;
return $self;
}
sub new_filehandle {
my ($class, $filehandle, %args) = @_;
my $self = bless [], $class;
$self->init_args(\%args);
if (exists $args{scalarref}
|| exists $args{arrayref} || exists $args{filename}) {
$self->_error_template_sources;
}
$args{filehandle} = $filehandle;
$args{path} = $self->build_path($args{path});
$self->set_filehandle( $args{filehandle} );
$args{cache} = 0;
$self->init_cache(\%args);
my $md5path = md5_hex(@{ $args{path} || [] });
$self->set_path( $args{path} );
$self->set_md5_path( $md5path );
if (my $t = $self->from_cache(\%args)) {
return $t;
}
$self->init(%args);
$self->from_scratch;
$self->init_includes;
return $self;
}
sub new_array_ref {
my ($class, $arrayref, %args) = @_;
if (exists $args{scalarref}
|| exists $args{filehandle} || exists $args{filename}) {
$class->_error_template_sources;
}
my $scalarref = \( join '', @$arrayref );
delete $args{arrayref};
return $class->new_scalar_ref($scalarref, %args);
}
sub new_scalar_ref {
my ($class, $scalarref, %args) = @_;
my $self = bless [], $class;
$self->init_args(\%args);
if (exists $args{arrayref}
|| exists $args{filehandle} || exists $args{filename}) {
$self->_error_template_sources;
}
$args{scalarref} = $scalarref;
$args{path} = $self->build_path($args{path});
$self->init_cache(\%args);
$self->set_scalar( $args{scalarref} );
my $text = $self->get_scalar;
my $md5 = md5($$text);
# if ($args{cache} and !$md5) {
# croak "For caching scalarrefs you need Digest::MD5";
# }
$self->set_filename($md5);
D && $self->log("md5: $md5");
my $md5path = md5_hex(@{ $args{path} || [] });
$self->set_path( $args{path} );
$self->set_md5_path( $md5path );
if (my $t = $self->from_cache(\%args)) {
return $t;
}
$self->init(%args);
$self->from_scratch;
$self->init_includes;
return $self;
}
sub init_includes {
my ($self) = @_;
my $includes = $self->get_includes;
my $cache = $self->get_cache_dir||'';
for my $fullpath (keys %$includes) {
my ($path, $filename, $htc) = @{ $includes->{$fullpath} };
D && $self->log("checking $fullpath ($filename) $htc?");
# TODO check $cache
$cache .= '-' . $self->get_md5_path;
#warn __PACKAGE__.':'.__LINE__.": init_includes() $filename\n";
if (not $htc or HTML::Template::Compiled::needs_new_check($cache||'',$filename, $self->get_expire_time)
) {
$htc = $self->new_from_object($path,$filename,$fullpath,$cache);
}
$includes->{$fullpath}->[2] = $htc;
$includes->{$fullpath}->[2]->set_plugins($self->get_plugins);
}
}
sub build_path {
my ($self, $path) = @_;
unless (defined $path) {
$path = [];
}
elsif (!ref $path) {
$path = [$path];
}
defined $ENV{'HTML_TEMPLATE_ROOT'}
and push @$path, $ENV{'HTML_TEMPLATE_ROOT'};
return $path;
}
sub from_scratch {
my ($self) = @_;
D && $self->log("from_scratch filename=".$self->get_filename);
my $fname = $self->get_filename;
if ( defined $fname and !$self->get_scalar and !$self->get_filehandle ) {
#D && $self->log("tried from_cache() filename=".$fname);
my $file = $self->createFilename( $self->get_path, \$fname );
D && $self->log("set_file $file ($fname)");
$self->set_file($file);
}
elsif ( defined $fname ) {
$self->set_file($fname);
}
D && $self->log( "compiling... " . $self->get_filename );
$self->compile();
return $self;
}
sub from_cache {
my ($self, $args) = @_;
my $t;
D && $self->log( "from_cache() filename=" . $self->get_filename );
$args ||= {};
my $plug = $args->{plugin} || [];
my $debug = $self->get_debug || $args->{debug};
# try to get memory cache
if ( $self->get_cache ) {
my $dir = $self->get_cache_dir;
$dir = '' unless defined $dir;
$dir .= '-' . $self->get_md5_path;
my $fname = $self->get_filename;
$t = $self->from_mem_cache($dir,$fname, $args);
if ($t) {
$t->set_args($args);
if (@$plug) {
$t->set_plugins($plug);
$t->load_plugins($plug);
}
if ($debug->{cache} & DEBUG_CACHE_MEM_HIT) {
warn "### HTML::Template::Compiled Cache Debug ### MEM CACHE HIT: $fname\n";
}
return $t;
}
# warn __PACKAGE__.':'.__LINE__.": not in mem cache: $fname\n";
if ($debug->{cache} & DEBUG_CACHE_MEM_MISS) {
warn "### HTML::Template::Compiled Cache Debug ### MEM CACHE MISS: @{[ $self->get_filename ]}\n";
}
}
D && $self->log( "from_cache() 2 filename=" . $self->get_filename );
# not in memory cache, try file cache
if ( $self->get_cache_dir ) {
my $file = $self->get_scalar || $self->get_filehandle
? $self->get_filename
: $self->createFilename( $self->get_path, \$self->get_filename );
my $dir = $self->get_cache_dir;
if (defined $dir and not -d $dir) {
croak "Cachedir '$dir' does not exist";
}
$t = $self->from_file_cache($dir, $file);
if ($t) {
$t->set_args($args);
if (@$plug) {
$t->set_plugins($plug);
$t->load_plugins($plug);
}
if ($debug->{cache} & DEBUG_CACHE_FILE_HIT) {
warn "### HTML::Template::Compiled Cache Debug ### FILE CACHE HIT: @{[ $self->get_filename ]}\n";
}
return $t;
}
if ($debug->{cache} & DEBUG_CACHE_FILE_MISS) {
warn "### HTML::Template::Compiled Cache Debug ### FILE CACHE MISS: @{[ $self->get_filename ]}\n";
}
}
D && $self->log( "from_cache() 3 filename=" . $self->get_filename );
return;
}
{
my $cache;
# {
# $cachedir => {
# $filename => $htc_object,
my $times;
sub needs_new_check {
my ($dir, $fname, $expire_time) = @_;
my $times = $times->{$dir}->{$fname} or return 1;
my $now = time;
return 0 if $now - $times->{checked} < $expire_time;
return 1;
}
sub from_mem_cache {
my ($self, $dir, $fname, $args) = @_;
my $cached = $cache->{$dir}->{$fname};
my $times = $times->{$dir}->{$fname};
D && $self->log("\$cached=$cached \$times=$times \$fname=$fname\n");
if ( $cached && $self->uptodate($times, $args) ) {
return $cached->clone;
}
D && $self->log("no or old memcache");
return;
}
sub _debug_cache {
my ($self) = @_;
my $dir = $self->get_cache_dir;
my $objects = $cache->{$dir};
my $times = $times->{$dir};
warn Data::Dumper->Dump([\$times], ['times']);
my @keys = keys %$objects;
warn Data::Dumper->Dump([\@keys], ['keys']);
}
sub add_mem_cache {
my ( $self, %times ) = @_;
D && $self->stack(1);
my $dir = $self->get_cache_dir;
$dir = '' unless defined $dir;
my @c = caller();
$dir .= '-' . $self->get_md5_path;
my $fname = $self->get_filename;
D && $self->log( "add_mem_cache $fname" );
my $clone = $self->clone;
$clone->clear_params();
my @plugs = @{ $self->get_plugins || [] };
for my $i (0 .. $#plugs) {
if (ref $plugs[$i]) {
if ($plugs[$i]->can('serialize')) {
$plugs[$i] = $plugs[$i]->serialize();
}
}
}
$clone->set_plugins(\@plugs);
$cache->{$dir}->{$fname} = $clone;
$times->{$dir}->{$fname} = \%times;
}
sub clear_cache {
my $dir = $_[0]->get_cache_dir;
# clear the whole cache
$cache = {}, $times = {}, return unless defined $dir;
# only specific directory
$cache->{$dir} = {};
$times->{$dir} = {};
}
sub clear_filecache {
my ( $self, $dir ) = @_;
defined $dir
or $dir = $self->get_cache_dir;
return unless -d $dir;
ref $self and $self->lock;
opendir my $dh, $dir or die "Could not open '$dir': $!";
my @files = grep { m/(\.pl|\.storable)$/ } readdir $dh;
for my $file (@files) {
my $file = File::Spec->catfile( $dir, $file );
unlink $file or die "Could not delete '$file': $!";
}
ref $self and $self->unlock;
return 1;
}
sub uptodate {
my ( $self, $cached_times, $args ) = @_;
return 1 if $self->get_scalar;
my $expire_time = $self->get_expire_time;
$expire_time = $args->{expire_time} unless defined $expire_time;
# unless ($cached_times) {
# my $dir = $self->get_cache_dir;
# $dir = '' unless defined $dir;
# my $fname = $self->get_filename;
# my $cached = $cache->{$dir}->{$fname};
# $cached_times = $times->{$dir}->{$fname};
# return unless $cached;
# }
my $now = time;
if ( $now - $cached_times->{checked} < $expire_time ) {
return 1;
}
else {
my $file = $self->createFilename( $self->get_path, \$self->get_filename );
$self->set_file($file);
#print STDERR "uptodate($file)\n";
my @times = $self->_checktimes($file);
if ( $times[MTIME] <= $cached_times->{mtime} ) {
D && $self->log("uptodate template old");
# set last check time to new value
$cached_times->{checked} = $now;
return 1;
}
}
# template is not up to date, re-compile it
return 0;
}
}
sub compile {
my ($self) = @_;
my ( $source, $compiled );
my $compiler = $self->get_compiler;
if ( my $file = $self->get_file and !$self->get_scalar ) {
D && $self->log( "compile from file " . $file );
die "Could not open '$file': $!" unless -f $file;
my @times = $self->_checktimes($file);
my $text = $self->_readfile($file);
my ( $source, $compiled ) = $compiler->compile( $self, $text, $file );
$self->set_perl($compiled);
$self->get_cache and $self->add_mem_cache(
checked => time,
mtime => $times[MTIME],
);
D && $self->log("compiled $file");
if ( $self->get_cache_dir ) {
D && $self->log("add_file_cache($file)");
$self->add_file_cache(
$source,
checked => time,
mtime => $times[MTIME],
);
}
}
elsif ( my $text = $self->get_scalar ) {
my $md5 = $self->get_filename; # yeah, weird
D && $self->log("compiled $md5");
my ( $source, $compiled ) = $compiler->compile( $self, $$text, $md5 );
$self->set_perl($compiled);
if ( $self->get_cache_dir ) {
D && $self->log("add_file_cache($file)");
$self->add_file_cache(
$source,
checked => time,
mtime => time,
);
}
}
elsif ( my $fh = $self->get_filehandle ) {
local $/;
my $data = <$fh>;
my ( $source, $compiled ) = $compiler->compile( $self, $data, '' );
$self->set_perl($compiled);
}
}
sub add_file_cache {
my ( $self, $source, %times ) = @_;
$self->lock;
my $cache = $self->get_cache_dir;
if (defined $cache and not -d $cache) {
croak "Cachedir '$cache' does not exist";
}
my $plfile = $self->escape_filename( $self->get_file );
my $filename = $self->get_filename;
my $lmtime = localtime $times{mtime};
my $lchecked = localtime $times{checked};
my $cachefile = "$cache/$plfile";
D && $self->log("add_file_cache() $cachefile");
{
require Storable;
require B::Deparse;
local $Storable::Deparse = 1;
my $clone = $self->clone;
$clone->prepare_for_cache;
my $v = $self->VERSION || '0.01';
my $to_cache = {
htc => $clone,
version => $v,
times => {
mtime => $times{mtime},
checked => $times{checked},
},
};
Storable::store($to_cache, "$cachefile.storable");
}
$self->unlock;
}
sub get_plugin {
my ($self, $class) = @_;
for my $plug (@{ $self->get_plugins || [] }) {
return $plug if (ref $plug || $plug) eq $class;
}
return;
}
sub from_file_cache {
my ($self, $dir, $file) = @_;
D && $self->stack;
D && $self->log("include file: $file");
my $escaped = $self->escape_filename($file);
my $req = File::Spec->catfile( $dir, "$escaped.storable" );
return unless -f $req;
return $self->include_file($req);
}
sub include_file {
my ( $self, $req ) = @_;
D && $self->log("do $req");
my $r;
my $t;
{
require Storable;
require B::Deparse;
local $Storable::Eval = 1;
my $cache;
eval {
$cache = Storable::retrieve($req);
};
#warn __PACKAGE__.':'.__LINE__.": error? $@\n";
return if $@;
my $cached_version = $cache->{version};
$t = $cache->{htc};
if (($t->VERSION || '0.01') ne $cached_version || !$t->uptodate( $cache->{times} )) {
# is not uptodate
return;
}
my $plug = $t->get_plugins || [];
$t->get_cache and $t->add_mem_cache(
checked => $cache->{times}->{checked},
mtime => $cache->{times}->{mtime},
);
}
return $t;
}
sub createFilename {
my ( $self, $path, $filename_ref, $cwd ) = @_;
my $filename = $$filename_ref;
D && $self->log("createFilename($path,$filename)");
D && $self->stack(1);
#warn __PACKAGE__.':'.__LINE__.": ---- createFilename($path, $$filename_ref, $cwd)\n";
if ($path) {
local $" = "\0";
my $cached = $PATHS{"@$path"}->{$filename};
return $cached if defined $cached;
}
if ( !$path or
(File::Spec->file_name_is_absolute($filename) &&
-f $filename) ) {
return $filename;
}
else {
D && $self->log( "file: " . File::Spec->catfile( $path, $filename ) );
if ($path && @$path) {
my @search = @$path;
for ( @search ) {
my $fp = File::Spec->catfile( $_, $filename );
if (-f $fp) {
local $" = "\0";
$PATHS{"@$path"}->{$filename} = $fp;
return $fp;
}
}
# not found in $path, try current template dir
if (defined $cwd) {
my $fp = File::Spec->catfile( $cwd, $filename );
if (-f $fp) {
for my $p (@search) {
if ($fp =~ m{^\Q$p\E(.*)}) {
my $rest = $1;
my (undef, @p) = File::Spec->splitdir($rest);
$rest = File::Spec->catfile(@p);
$$filename_ref = $rest;
$PATHS{"@$path"}->{$rest} = $fp;
}
}
return $fp;
}
}
}
elsif (-f $filename) {
$PATHS{''}->{$filename} = $filename;
return $filename;
}
# TODO - bug with scalarref
croak "'$filename' not found";
}
}
sub dump {
my ( $self, $var ) = @_;
require Data::Dumper;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Sortkeys = 1;
return Data::Dumper->Dump( [$var], ['DUMP'] );
}
sub dump_var {
my ($class, $var, $varname) = @_;
local $Data::Dumper::Terse = 0;
local $Data::Dumper::Indent = 2;
local $Data::Dumper::Purity = 0;
local $Data::Dumper::Pad = "";
local $Data::Dumper::Useqq = 0;
local $Data::Dumper::Deepcopy = 0;
local $Data::Dumper::Quotekeys = 1;
local $Data::Dumper::Bless = 'bless';
local $Data::Dumper::Pair = ' => ';
local $Data::Dumper::Maxdep = 0;
local $Data::Dumper::Useperl = 0;
local $Data::Dumper::Sortkeys = 1;
return Data::Dumper->Dump( [$var], [$varname] );
}
sub init_cache {
my ($self, $args) = @_;
my $cachedir = $args->{file_cache_dir};
if ($args->{file_cache}) {
$self->set_cache_dir($cachedir) if $args->{file_cache};
}
$self->set_cache( exists $args->{cache} ? $args->{cache} : 1 );
}
sub init_args {
my ($self, $args) = @_;
if (exists $args->{cache_dir}) {
# will soon be deprecated
$args->{file_cache_dir} = delete $args->{cache_dir};
unless (exists $args->{file_cache}) {
# warn in future versions
$args->{file_cache} = 1;
}
}
if ($args->{plugin} and (ref $args->{plugin}) ne 'ARRAY') {
$args->{plugin} = [$args->{plugin}];
}
my $debug_cache_args = delete $args->{cache_debug} || 0;
my $debug_cache = 0;
if ($debug_cache_args) {
unless (ref $debug_cache_args) {
# no array ref, just a true value
$debug_cache |= DEBUG_CACHE_FILE_MISS | DEBUG_CACHE_FILE_HIT | DEBUG_CACHE_MEM_MISS | DEBUG_CACHE_MEM_HIT;
}
else {
for my $opt (@$debug_cache_args) {
if ($opt eq 'file_miss') {
$debug_cache |= DEBUG_CACHE_FILE_MISS;
}
elsif ($opt eq 'file_hit') {
$debug_cache |= DEBUG_CACHE_FILE_HIT;
}
elsif ($opt eq 'mem_miss') {
$debug_cache |= DEBUG_CACHE_MEM_MISS;
}
elsif ($opt eq 'mem_hit') {
$debug_cache |= DEBUG_CACHE_MEM_HIT;
}
}
}
}
# check deprecated
for (qw(method_call deref formatter_path default_path formatter)) {
if (exists $args->{$_}) {
croak "Option $_ is deprecated, see documentation";
}
}
if (exists $args->{dumper}) {
croak "Option dumper is deprecated, use a plugin instead";
}
my $debug_file = delete $args->{debug_file} || 0;
my $debug_compiled = delete $args->{debug} ? 1 : 0;
my $debug = 0;
$debug |= DEBUG_COMPILED if $debug_compiled;
$args->{debug} = {
options => $debug,
file => $debug_file,
cache => $debug_cache,
};
my %optimize = (
initial_var => 1,
object_check => 0,
root_hash => 0,
%{ $args->{optimize} || {} },
);
%$args = (
search_path_on_include => $SEARCHPATH,
loop_context_vars => 0,
case_sensitive => $CASE_SENSITIVE_DEFAULT,
# debug_file => 0,
objects => 'strict',
out_fh => 0,
global_vars => 0,
default_escape => $DEFAULT_ESCAPE,
default_path => PATH_DEREF,
use_query => $DEFAULT_QUERY,
#use_expressions => 0,
use_perl => 0,
open_mode => '',
no_includes => 0,
pre_chomp => 0,
post_chomp => 0,
expire_time => $NEW_CHECK,
strict => 1,
optimize => \%optimize,
%$args,
);
$self->set_args($args);
# return %defaults;
}
sub init {
my ( $self, %args ) = @_;
$self->set_expire_time($args{expire_time});
$self->set_loop_context(1) if $args{loop_context_vars};
$self->set_case_sensitive( $args{case_sensitive} );
$self->set_default_escape( $args{default_escape} );
$self->set_default_path( $args{default_path} );
$self->set_use_query( $args{use_query} );
$self->set_chomp([$args{pre_chomp}, $args{post_chomp}]);
$self->set_strict( $args{strict} );
$self->set_optimize($args{optimize});
my $warnings = $args{warnings} || 0;
unless ($warnings eq 1 or $warnings eq 'fatal') {
$warnings = 0;
}
$self->set_warnings($warnings);
my $line_info = 0;
if ($args{line_info}) {
$line_info = 1;
}
$self->set_line_info($line_info);
#$self->set_use_expressions( $args{use_expressions} );
if ($args{use_expressions}) {
require HTML::Template::Compiled::Expr;
}
$args{open_mode} = '' unless length $args{open_mode};
if ($args{open_mode}) {
$args{open_mode} =~ s/^[<>]//; # <:utf8
}
$self->set_open_mode( $args{open_mode} );
$self->set_search_path( $args{search_path_on_include} );
$self->set_includes({});
if ( $args{filter} ) {
require HTML::Template::Compiled::Filter;
$self->set_filter(
HTML::Template::Compiled::Filter->new( $args{filter} ) );
}
$self->set_debug( $args{debug} );
$self->set_debug_file( $args{debug_file} );
$self->set_objects( $args{objects} );
$self->set_out_fh( $args{out_fh} );
$self->set_global_vars( $args{global_vars} );
if (my $plugins = $args{plugin}) {
$self->set_plugins($plugins);
}
my $compiler = $self->compiler_class->new;
$self->set_compiler($compiler);
my $tagstyle = $args{tagstyle};
my $parser;
if (ref $tagstyle eq 'ARRAY') {
# user specified named styles or regexes
$parser = $self->parser_class->new(
tagstyle => $tagstyle,
use_expressions => $args{use_expressions},
strict => $args{strict},
);
$parser->set_perl($args{use_perl});
}
$args{parser} = ${$args{parser}} if ref $args{parser} eq 'REF';
if (UNIVERSAL::isa($args{parser}, 'HTML::Template::Compiled::Parser')) {
$parser = $args{parser};
}
unless ($parser) {
$parser ||= $self->parser_class->default();
$parser->set_perl($args{use_perl});
$parser->set_expressions($args{use_expressions});
$parser->set_strict($args{strict});
}
$parser->set_chomp([$args{pre_chomp}, $args{post_chomp}]);
if ($args{use_perl}) {
$parser->add_tagnames({
HTML::Template::Compiled::Token::OPENING_TAG() => {
PERL => [sub { 1 }],
}
});
}
if ($args{no_includes}) {
$parser->remove_tags(qw/ INCLUDE INCLUDE_VAR INCLUDE_STRING /);
}
$self->set_parser($parser);
if (my $plugins = $self->get_plugins) {
$self->init_plugins($plugins);
$self->set_plugins($plugins);
}
}
{
my %_plugins;
sub load_plugins {
my ($self, $plugins) = @_;
for my $plug (@$plugins) {
next if ref $plug;
next if $_plugins{$plug};
if ($plug =~ m/^::/) {
$plug = "HTML::Template::Compiled::Plugin$plug";
}
next if $_plugins{$plug};
unless ($plug->can('register')) {
eval "require $plug";
if ($@) {
carp "Could not load plugin $plug\n";
}
}
$_plugins{$plug} = 1;
}
}
}
sub init_plugins {
my ($self, $plugins) = @_;
$self->load_plugins($plugins);
my $parser = $self->get_parser;
my $compiler = $self->get_compiler;
for my $plug (@$plugins) {
my $actions = $self->get_plugin_actions($plug);
if (my $tagnames = $actions->{tagnames}) {
$parser->add_tagnames($tagnames);
}
if (my $escape = $actions->{escape}) {
$compiler->add_escapes((ref $plug) || $plug, $escape);
}
if (my $tags = $actions->{compile}) {
$compiler->add_tags($tags);
}
}
}
{
my $classes = {};
sub register {
my ($class, $plugins) = @_;
$plugins = [$plugins] unless ref $plugins eq 'ARRAY';
for my $plug (@$plugins) {
my $actions = $plug->register;
my $plug_class = (ref $plug) || $plug;
$classes->{ $plug_class} = $actions;
HTML::Template::Compiled::Compiler->setup_escapes($plug_class, $actions->{escape}||{});
}
}
sub get_plugin_actions {
my ($self, $pclass) = @_;
return $classes->{ref $pclass || $pclass};
}
}
sub _readfile {
my ( $self, $file ) = @_;
my $open_mode = $self->get_open_mode;
open my $fh, "<$open_mode", $file or die "Cannot open '$file': $!";
local $/;
<$fh>;
}
sub get_code {
return $_[0]->get_perl;
}
sub compile_early { 1 }
sub method_call { '.' }
sub deref { '.' }
sub formatter_path { '/' }
sub parser_class { 'HTML::Template::Compiled::Parser' }
sub compiler_class { 'HTML::Template::Compiled::Compiler' }
sub quote_file {
defined(my $f = $_[1]) or return '';
$f =~ s/'/\\'/g;
return qq/'$f'/;
}
# this method gets a varname like 'var' or 'object.method'
# or 'hash.key' and makes valid perl code out of it that will
# be eval()ed later
# so assuming . is the character for dereferencing hashes the string
# hash.key (found inside <tmpl_var name="hash.key">) will be converted to
# '$t->get_var($P, $$C, 1, [PATH_DEREF, 'key'])'
# the get_var method walks the paths given through the data structure.
# $P is the parameter hash of the template, $C is a reference to the current
# parameter hash. the third argument to get_var is 'final'.
# <tmpl_var foo> is a 'final' path, and <tmpl_with foo> is not.
# so final means it's in 'print-context'.
# -------- warning, ugly code
# i'm trading maintainability for efficiency here
sub try_global {
my ( $self, $walk, $path ) = @_;
my $stack = $self->get_globalstack || [];
#warn Data::Dumper->Dump([\$stack], ['stack']);
for my $item ( $walk, reverse @$stack ) {
if (my $code = UNIVERSAL::can($item, $path)) {
my $r = $code->($item);
return $r;
}
else {
next unless exists $item->{$path};
return $item->{$path};
}
}
return;
}
{
sub _walk_formatter {
my ($self, $walk, $key, $global) = @_;
my $ref = ref $walk;
my $fm = $HTML::Template::Compiled::Formatter::formatter;
my $sub = exists $fm->{$ref} ? $fm->{$ref}->{$key} : undef;
my $stack = [];
my $new_walk;
if ($global) {
$stack = $self->get_globalstack || [];
}
for my $item ($walk, reverse @$stack) {
#print STDERR "::::::: formatter $walk -> $key (sub=$sub)\n";
if (defined $sub) {
$new_walk = $sub->($walk);
last;
}
elsif (exists $item->{$key}) {
#print STDERR "===== \$item->{$key} exists! '$item->{$key}'\n";
$new_walk = $item->{$key};
last;
}
# try next item in stack
}
#print STDERR "---- formatter $walk\n";
return $new_walk;
}
# ----------- still ugly code
# not needed anymore
# if (my $formatter = $self->get_formatter() and $final and my $ref = ref $walk) {
# if (my $sub = $formatter->{$ref}->{''}) {
# my $return = $sub->($walk,$self,$P);
# return $return unless ref $return;
# }
# }
# return $walk;
}
# end ugly code, phooey
# returns if the var is valid
# only allow '.', '/', '+', '-' and '_'
# fix 2007-07-23: HTML::Template allows every character
# although the documentation says it doesn't.
sub validate_var {
return 1;
#return $_[1] !~ tr{a-zA-Z0-9._[]/#-}{}c;
}
sub escape_filename {
my ( $t, $f ) = @_;
$f =~ s#([/:\\])#'%'.uc sprintf"%02x",ord $1#ge;
return $f;
}
sub _checktimes {
my $self = shift;
D && $self->stack;
my $filename = shift;
my $mtime = ( stat $filename )[9];
#print STDERR "stat $filename = $mtime\n";
my $checked = time;
my $lmtime = localtime $mtime;
my $lchecked = localtime $checked;
return ( $mtime, $checked, $lmtime, $lchecked );
}
sub clone {
my ($self) = @_;
return bless [@$self], ref $self;
}
sub new_scalar_from_object {
my ($self, $scalar) = @_;
my $new = $self->clone;
$new->set_includes({});
$new->set_perl(undef);
$new->set_filehandle();
$new->set_cache(0);
$new->set_cache_dir(undef);
$new->set_scalar(\$scalar);
my $md5 = md5($scalar);
$new->set_filename($md5);
$new = $new->from_scratch;
return $new;
}
# create from existing object (TMPL_INCLUDE)
sub new_from_object {
my ( $self, $path, $filename, $fullpath, $cache ) = @_;
unless (defined $filename) {
my ($file) = (caller(1))[3];
croak "Filename is undef (in template $file)";
}
my $new = $self->clone;
D && $self->log("new_from_object($path,$filename,$fullpath,$cache)");
$new->set_filename($filename);
#if ($fullpath) {
# $self->set_file($fullpath);
#}
$new->set_includes({});
$new->set_scalar();
$new->set_filehandle();
my $md5path = md5_hex(@{ $path || [] });
$new->set_path($path);
$new->set_md5_path( $md5path );
$new->set_perl(undef);
if (my $cached = $new->from_cache($self->get_args)) {
$cached->set_plugins($self->get_plugins);
$cached->init_includes;
return $cached
}
unless ($new->get_compiler) {
my %args = %{ $self->get_args || {} };
$new->init(%args);
}
$new = $new->from_scratch;
$new->init_includes;
return $new;
}
sub prepare_for_cache {
my ($self) = @_;
$self->clear_params;
my @plugs = @{ $self->get_plugins || [] };
for my $i (0 .. $#plugs) {
if (ref $plugs[$i]) {
if ($plugs[$i]->can('serialize')) {
$plugs[$i] = $plugs[$i]->serialize();
}
}
}
$self->set_plugins(\@plugs);
my $includes = $self->get_includes;
for my $fullpath (keys %$includes) {
my ($path, $filename, $htc) = @{ $includes->{$fullpath} };
$includes->{$fullpath} = [$path, $filename];
}
$self->set_parser(undef);
$self->set_compiler(undef);
$self->set_args(undef);
$self->set_globalstack(undef);
}
sub preload {
my ( $class, $dir ) = @_;
opendir my $dh, $dir or die "Could not open '$dir': $!";
my @files = grep { m/\.pl|\.storable$/ } readdir $dh;
closedir $dh;
my $loaded = 0;
for my $file (@files) {
my $success = $class->include_file( File::Spec->catfile( $dir, $file ) );
$loaded++ if $success;
}
return scalar $loaded;
}
sub precompile {
my ($class, %args) = @_;
my $files = delete $args{filenames};
return unless ref $files eq 'ARRAY';
my @precompiled;
for my $file (@$files) {
my $htc = $class->new(%args,
(ref $file eq 'SCALAR'
? 'scalarref'
: ref $file eq 'ARRAY'
? 'arrayref'
: ref $file eq 'GLOB'
? 'filehandle'
: 'filename') => $file,
);
push @precompiled, $htc,
}
return \@precompiled;
}
sub clear_params {
$_[0]->[PARAM] = ();
}
sub get_param {
return $_[0]->[PARAM];
}
sub param {
my $self = shift;
if (!@_) {
return $self->query();
return UNIVERSAL::can($self->[PARAM],'can')
? $self->[PARAM]
: $self->[PARAM]
? keys %{$self->[PARAM]}
: ();
}
my %p;
if (@_ == 1) {
if ( ref $_[0] ) {
# feed a hashref or object
if (ref $_[0] eq 'HASH') {
# hash, no object
%p = %{ $_[0] };
}
else {
$self->[PARAM] = $_[0];
return;
}
}
else {
# query a parameter
return $self->[PARAM]->{ $_[0] };
}
}
else {
%p = @_;
}
if ( !$self->get_case_sensitive ) {
my $lc = $self->lchash( {%p} );
%p = %$lc;
}
$self->[PARAM]->{$_} = $p{$_} for keys %p;
}
sub query {
my ($self, $what, $tags) = @_;
# param() no arguments should behave like query
# query() is not activated by default, and
# my %param = (); $htc->param(%param); should
# *not* call query(). so we check if the user wants
# a return value; that indicates that they wanted to
# use query-like behaviour.
return unless defined wantarray();
#print STDERR "query(@_)\n";
my $info = $self->get_parse_tree
or do {
$self->_error_no_query();
return;
};
unless (ref $info) {
# not compiled yet!
$self->_error_not_compiled();
return;
}
my $pointer = {children => $info};
$tags = [] unless defined $tags;
$tags = [$tags] unless ref $tags eq 'ARRAY';
my $includes = $self->get_includes;
my %include_info = map {
$includes->{$_}->[1] => $includes->{$_}->[2]->get_parse_tree;
} keys %{ $includes };
for my $tag (@$tags) {
my $value;
my %includes = map {
my $item = $pointer->{children}->{$_};
($item->{type} eq 'INCLUDE' and $include_info{$_})
? (%{$include_info{$_}})
: ()
} keys %{ $pointer->{children} };
if (defined ($value = $pointer->{children}->{lc $tag})) {
$pointer = $value;
}
elsif (defined ($value = $includes{lc $tag})) {
$pointer = $value;
}
else {
return;
}
}
unless ($what) {
my @return = map {
my $item = $pointer->{children}->{$_};
($item->{type} eq 'INCLUDE' and $include_info{$_})
? (keys %{$include_info{$_}})
: $_;
} keys %{ $pointer->{children} };
return @return;
}
elsif ($what eq 'name') {
my $type = $pointer->{type};
return $type;
}
elsif ($what eq 'loop') {
if ($pointer->{type} eq 'LOOP') {
my @return = map {
my $item = $pointer->{children}->{$_};
($item->{type} eq 'INCLUDE' and $include_info{$_})
? (keys %{$include_info{$_}})
: $_;
} keys %{ $pointer->{children} };
return @return;
}
else { croak "error: (@$tags) is not a LOOP" }
}
return;
}
# =head2 lchash
#
# my $capped_href = $self->lchash(\%href);
#
# Input:
# - hashref or arrayref of hashrefs
#
# Output: Returns a reference to a cloned data structure where all the keys are
# capped.
#
# =cut
sub lchash {
my ( $self, $data ) = @_;
my $lc;
if ( ref $data eq 'HASH' ) {
for my $key ( keys %$data ) {
my $uc_key = lc $key;
my $val = $self->lchash( $data->{$key} );
$lc->{$uc_key} = $val;
}
}
elsif ( ref $data eq 'ARRAY' ) {
for my $item (@$data) {
my $new = $self->lchash($item);
push @$lc, $new;
}
}
else {
$lc = $data;
}
return $lc;
}
sub output {
my ( $self, $fh ) = @_;
my $p = $self->[PARAM] || {};
# if we only have an object as parameter
$p = ref $p eq 'HASH'
? \% { $p }
: $p;
my $f = $self->get_file;
$fh = \*STDOUT unless $fh;
if ($DEBUG) {
my $output;
eval {
$output = $self->get_perl()->( $self, $p, \$p, $fh );
};
if ($@) {
$LAST_EXCEPTION = $@;
my $filename = $self->get_file;
die "Error while executing '$filename': $@";
}
return $output;
}
else {
$self->get_perl()->( $self, $p, \$p, $fh );
}
}
sub import {
my ( $class, %args ) = @_;
if ( $args{compatible} ) {
carp "Usage of use option 'compatible' is deprecated";
$class->CaseSensitive(0);
$class->SearchPathOnInclude(0);
$class->UseQuery(1);
}
elsif ( $args{speed} ) {
carp "Usage of use option 'speed' is deprecated";
# default at the moment
$class->CaseSensitive(1);
$class->SearchPathOnInclude(1);
$class->UseQuery(0);
}
if (exists $args{short}) {
carp "Usage of use option 'short' is deprecated";
__PACKAGE__->export_to_level(1, scalar caller(), 'HTC');
}
}
sub var2expression {
my ($self, $var) = @_;
$var = $self->get_compiler->parse_var($self,
var => $var,
method_call => $self->method_call,
deref => $self->deref,
formatter_path => $self->formatter_path,
);
return $var;
}
sub ExpireTime {
my ($class, $seconds) = @_;
$NEW_CHECK = $seconds;
}
sub EnableSub {
carp "Warning: Subref variables are not supported any more, use HTML::Template::Compiled::Classic instead";
}
sub CaseSensitive {
my ($class, $bool) = @_;
$CASE_SENSITIVE_DEFAULT = $bool ? 1 : 0;
}
sub SearchPathOnInclude {
my ($class, $bool) = @_;
$SEARCHPATH = $bool ? 1 : 0;
}
sub UseQuery {
my ($class, $bool) = @_;
$DEFAULT_QUERY = $bool ? 1 : 0;
}
sub pushGlobalstack {
my $stack = $_[0]->get_globalstack;
push @$stack, $_[1];
$_[0]->set_globalstack($stack);
}
sub popGlobalstack {
my $stack = $_[0]->get_globalstack;
pop @$stack;
$_[0]->set_globalstack($stack);
}
{
my $lock_fh;
sub lock {
my $file = File::Spec->catfile( $_[0]->get_cache_dir, "lock" );
unless ( -f $file ) {
# touch
open $lock_fh, '>', $file
or croak "Could not open lockfile '$file' for writing: $!";
close $lock_fh;
}
open $lock_fh, '+<', $file
or croak "Could not open lockfile '$file' for read/write: $!";
flock $lock_fh, LOCK_EX;
}
sub unlock {
close $lock_fh;
}
}
{
my $loaded = 0;
my $error = 0;
sub require_storable {
return 1 if $loaded;
return 0 if $error;
eval {
require Storable;
};
if ($@) {
$error = 1;
return 0;
}
eval "use B::Deparse 0.61";
if ($@) {
$error = 1;
return 0;
}
return 1;
}
}
sub debug_code {
my ($self, $html) = @_;
my $perl = $self->get_perl;
require B::Deparse;
my $deparse = B::Deparse->new("-p", "-sC");
my $body = $deparse->coderef2text($perl);
my $filename = $self->get_file;
#warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$body], ['body']);
my $message = '';
if ($LAST_EXCEPTION and $LAST_EXCEPTION =~ m/at (?:\(eval \d*\)|\S+) line (\d+)\./) {
my $rline = $1;
my $line = $rline;
$line--;
my @lines = split m#$/#, $body;
if ($line > $#lines) {
$line = $#lines;
}
my $pre = $line > 0 ? join $/, @lines[0 .. $line - 1] : '';
my $post = $line < $#lines ? join $/, @lines[$line + 1 .. $#lines] : '';
my $error = "$/$/# ------------------- ERROR line $rline in template $filename -----------------$/";
my $last = $LAST_EXCEPTION;
$LAST_EXCEPTION =~ s#$/# #g;
$error .= "# $last$/$lines[$line]$/";
if ($html) {
for ($pre, $error, $post) {
s/</</g;
s/>/>/g;
}
$message = <<"EOM";
<table border="0" style="background-color: #eeeeee;"><tr><td><pre>$pre</pre></td></tr>
<tr><td style="background-color: #ffffff; color: #ff0000"><pre>$error</pre></td></tr>
<tr><td><pre>$post</pre></td></tr></table>
EOM
}
else {
$message .= $pre;
$message .= $error;
$message .= $post;
}
}
else {
$message = $LAST_EXCEPTION;
}
return $message;
}
1;
__END__
=pod
=head1 NAME
HTML::Template::Compiled - Template System Compiles HTML::Template files to Perl code
=head1 SYNOPSIS
use HTML::Template::Compiled;
# recommended options:
# case_sensitive => 1
# search_path_on_include => 1
# use_query => 0
# default_escape => 'HTML' # <-- HIGHLY RECOMMENDED
# note that the following
# use HTML::Template::Compiled speed => 1
# is deprecated (can be problematic under persistent environments)
# or for the biggest compatibility with HTML::Template
# case_sensitive => 0
# search_path_on_include => 0
# use_query => 1
# note that the following
# use HTML::Template::Compiled compatible => 1;
# is deprecated (can be problematic under persistent environments)
# or use HTML::Template::Compiled::Classic
my $htc = HTML::Template::Compiled->new(
filename => 'test.tmpl',
case_sensitive => 1,
default_escape => 'HTML',
);
$htc->param(
BAND => $name,
ALBUMS => [
{ TITLE => $t1, YEAR => $y1 },
{ TITLE => $t2, YEAR => $y2 },
],
);
print $htc->output;
test.tmpl:
Band: <TMPL_VAR BAND>
<TMPL_LOOP ALBUMS>
Title: <TMPL_VAR TITLE> (<TMPL_VAR YEAR>)
</TMPL_LOOP>
Or use different tag styles:
Band: <%= BAND %>
<%loop ALBUMS %>
Title: <%= TITLE %> (<%= YEAR %>)
<%/loop %>
Band: [%= BAND %]
[%loop ALBUMS %]
Title: [%= TITLE %] ([%= YEAR %])
[%/loop %]
=head1 DESCRIPTION
HTML::Template::Compiled is a template system which can be used for
L<HTML::Template> templates with almost the same API. It
offers more flexible template delimiters, additional tags and features,
and by compiling the template into perl code it can run significantly faster
in persistent environments such as FastCGI or mod_perl.
The goal is to offer more features for flexibility but keep the basic syntax
as easy as it is.
Features at a glance:
=over 4
=item Dot notation for objects, hashes and arrays
=item Use expressions without any disadvantages like those in L<HTML::Template::Expr>
=item Write escaping plugins and plugins for new tags
=item Alternate delimiters, e.g. C<[%if %]> and C<< <%if %> >>
=item Avoid C<global_vars> option by using the C<SET_VAR> tag to create aliases.
=item Tags ELSIF, EACH, WHILE, COMMENT, WRAPPER, SWITCH/CASE, INCLUDE_VAR
=item Chomp newlines/whitespace
=back
For a quick reference, see L<HTML::Template::Compiled::Reference>.
As the basic features work like in L<HTML::Template>, please get familiar
with its documentation before.
HTML::Template::Compiled (HTC) does not implement all features of
L<HTML::Template> (see L<"COMPATIBILITY">), and
it has got some additional features which are explained below:
L<"ADDITIONAL FEATURES">
See L<"BENCHMARKS"> for some examples on the performance. Since it depends
highly on the options used and on the template size there can be no general
statement on its performance.
You might want to use L<HTML::Template::Compiled::Lazy> for CGI environments
as it doesn't parse the template before calling output. But note that HTC::Lazy
isn't much tested, and I don't use it myself, so there's a lack of experience.
If you use it and have problems, please report.
HTC will use a lot of memory because it keeps all template objects in memory.
If you are on mod_perl, and have a lot of templates, you should preload them at server
startup to be sure that it is in shared memory. At the moment HTC is not fully tested for
keeping all data in shared memory (e.g. when a copy-on-write occurs),
but it seems like it's behaving well.
For preloading you can use
HTML::Template::Compiled->preload($cache_dir).
Generating code, writing it on disk and later eval() it can open security holes, for example
if you have more users on the same machine that can access the same files (usually an
http server running as 'www' or 'nobody'). See L<"SECURITY"> for details what you can
do to safe yourself.
NOTE: If you don't need any of the additional features listed below and if you don't
need the speed (in many cases it's probably not worth trading speed for memory), then
you might be better off with just using HTML::Template.
NOTE2: If you have any questions, bug reports, send them to me and not to Sam Tregar.
This module is developed by me at the moment, independently from HTML::Template, although
I try to get most of the tests from it passing for HTC. See L<"RESOURCES"> for
current information.
=head2 FEATURES FROM HTML::TEMPLATE
=over 4
=item TMPL_VAR
=item TMPL_LOOP
=item TMPL_(IF|UNLESS|ELSE)
=item TMPL_INCLUDE
=item HTML_TEMPLATE_ROOT
=item ESCAPE=(HTML|URL|JS|0)
=item DEFAULT=...
=item C<__first__>, C<__last__>, C<__inner__>, C<__outer__>, C<__odd__>, C<__counter__>, C<__even__>
=item <!-- TMPL_VAR NAME=PARAM1 -->
=item case insensitive var names
use option case_sensitive => 0 to use this feature (slow down)
=item filters
=item vars that are subrefs - not implemented, only in HTML::Template::Compiled::Classic
=item scalarref, arrayref, filehandle
=item C<global_vars>
=item C<query>
Has a bug (doesn't return parameters in included files of included files).
I'm working on that.
=back
=head2 ADDITIONAL FEATURES
What can HTC do for you additionally to HTML::Template?
=over 4
=item tag TMPL_ELSIF
No need to have cascading "if-else-if-else"s
=item tag TMPL_EACH
Iterate over a hash. See L<"TMPL_EACH">
=item tag TMPL_WITH
see L<"TMPL_WITH">
=item tag TMPL_WHILE
see L<"TMPL_WHILE">
=item tag TMPL_SET_VAR
see L<"SET_VAR">
=item tag TMPL_USE_VARS
see L<"USE_VARS">
=item tags TMPL_COMMENT, TMPL_NOPARSE, TMPL_VERBATIM
see L<"TMPL_COMMENT">, L<"TMPL_NOPARSE">, L<"TMPL_VERBATIM">
=item tag TMPL_WRAPPER
see L<"WRAPPER">
=item C<__index__>
Additional loop variable (C<__counter__ -1>)
=item C<__break__>
Additional loop variable (see L<"TMPL_LOOP">)
=item C<__filename__>, C<__filenameshort__> (since 0.91_001)
Insert the template filename for debugging:
<%= __filename__ %>
<%= __filenameshort__ %>
will turn out as:
templates/path/file.html
path/file.html
See also option debug_file in L<"OPTIONS"> for adding the filename globally.
=item tags TMPL_SWITCH, TMPL_CASE
see L<"TMPL_SWITCH">
=item C<TMPL_PERL>
Include perl code in your template. See L<"RUNNING PERL WITH TMPL_PERL">
=item CHOMP
New in version 0.96_001, please report any bugs and send me suggestions.
You can set global chomp options in the constructor. These work like in
Template-Toolkit:
my $htc = HTML::Template::Compiled->new(
pre_chomp => 0, # 0, 1, 2, 3, default 0
post_chomp => 1, # 0, 1, 2, 3, default 0
);
Meaning of the values:
0: Don't chomp
1: remove only spaces in the line before or after the tag
2: remove all whitespaces before or after the tag, and replace with one space
3: remove all whitespaces before or after the tag
In the template you can change that feature by using PRE_CHOMP and POST_CHOMP
attributes:
<%= foo PRE_CHOMP=3 POST_CHOMP=1 %>
The experimental tags +..._chomp have been removed.
=item Generating perl code
See L<"IMPLEMENTATION">
=item better variable access
dot-notation for accessing hash values. See L<"EXTENDED VARIABLE ACCESS">
=item rendering objects
dot-notation for accessing object methods. See L<"RENDERING OBJECTS">
=item output to filehandle
See L<"OPTIONS">
=item Dynamic includes
C<INCLUDE_VAR>, C<INCLUDE_STRING>. See L<"INCLUDE">
=item tag TMPL_IF_DEFINED
Check for definedness instead of truth:
<TMPL_IF_DEFINED NAME="var">
=item ALIAS
Set an alias for a loop variable. You can use the alias then with C<$alias>.
The syntax without the C<$> is also possible but not recommended any more.
For example, these two loops are functionally equivalent:
<tmpl_loop foo>
<tmpl_var _>
</tmpl_loop foo>
<tmpl_loop foo alias=current>
<tmpl_var $current>
</tmpl_loop foo>
This works with C<TMPL_LOOP> and C<TMPL_WHILE> at the moment.
You can also set aliases with the C<SET_VAR> tag. See L<"SET_VAR">
To use template parameters with a C<$> at the beginning (which is not
officially supported, but some are obviously using it), you can set:
local $HTML::Template::Compiled::Compiler::DISABLE_NEW_ALIAS = 1;
This is only a temporary workaround and will be removed some day!
Note that you are also able to access variables with dollar signs like this:
<tmpl_var _.$foo >
since underscore means current position in the parameter stash, and aliases
are only recognized at the beginning of a template var. But note that
dollar signs are still not officially supported.
=item Chained escaping
See L<"ESCAPING">
=item tagstyles
For those who like it (i like it because it is shorter than TMPL_), you
can use E<lt>% %E<gt> tags and the E<lt>%= tag instead of E<lt>%VAR (which will work, too):
<%IF blah%> <%= VARIABLE%> <%/IF%>
Define your own tagstyles and/or deactivate predefined ones.
See L<"OPTIONS"> tagstyle.
=item pre_chomp, post_chomp
See L<"CHOMP">
=back
=head2 MISSING AND DIFFERENT FEATURES
There are some features of H::T that are missing or behaving different.
I'll try to list them here.
=head3 MISSING FEATURES
=over 4
=item die_on_bad_params
I don't think I'll implement that.
=item force_untaint
Not planned at the moment
=item vanguard_compatibility_mode
Not planned.
=item shared_cache, double_cache
Not planned at the moment
=item blind_cache
Not sure if I should implement. In HTC you have the possibility to
set the expire time of the templates (after that time in memory the
template file is rechecked if it has changed), so setting a very high
value for expire_time would have the same effect as blind_cache.
See L<"CACHING"> C<expire_time>
=item double_file_cache
If I understand correctly, in HT, this enables memory and file cache at
the same time. In HTC, this is not needed. If you use file_cache and cache,
both are used.
=item file_cache_dir_mode
Not planned. The cache dir must exist, and subdirectories are
not created at the moment.
=item cache_lazy_vars, cache_lazy_loops
Not planned at the moment (This would be for HTML::Template::Compiled::Classic,
since it implements code refs).
=item utf8
Might be added in the future, HTC already has C<open_mode>
=item various debug options
Might be implemented in the future
=item associate
Not planned.
=item max_includes
Not planned
=item die_on_missing_include
Maybe
=back
=head3 DIFFERENT FEATURES
=over 4
=item case_sensitive
default is 1 (on).
Deactivate by passing option case_sensitive 0.
Note (again): this will slow down templating a lot (50%).
Explanation: This has nothing to do with C<TMPL_IF> or C<tmpl_if>. It's
about the variable names. With case_sensitive set to 1, the following
tags are different:
<tmpl_var Foo> prints the value of hash key 'Foo'
<tmpl_var fOO> prints the value of hash key 'fOO'
With case_sensitive set to 0, all your parameters passed to C<param()>
are converted to lowercase, and the following tags are the same:
<tmpl_var Foo> prints the value of hash key 'foo'
<tmpl_var fOO> prints the value of hash key 'foo'
=item subref variables
As of version 0.69, subref variables are not supported any more with
HTML::Template::Compiled. Use L<HTML::Template::Compiled::Classic>
(contained in this distribution) instead. It provides most features
of HTC.
=item search_path_on_include
Default: 0
In the HTML::Template documentation it says, if search_path_on_include
is set to 1, the paths of the path option are searched, while the default
behaviour is to look "only" in the current template directory.
It's not clear if it still searches in the current directory if set
to 1. I found out that it is not, so you cannot have both.
In HTML::Template::Compiled, search_path_on_include can have three values:
0: search current template directory
1: search paths specified
2: search paths and current template directory.
=item open_mode
In HTC you should leave out the C<<> at the beginning.
If you want to have your templates read in utf-8, use
open_mode => ':encoding(utf-8)',
as an option.
=item use_query
default is 0 (off). Set it via the option
C<use_query>
=item Arrayrefs
At the moment this snippet
<tmpl_if arrayref>true<tmpl_else>false</tmpl_if arrayref>
with this code:
$htc->param(arrayref => []);
will print true in HTC and false in HTML::Template. In HTML::Template an
array is true if it has content, in HTC it's true if it (the reference) is
defined. I'll try to find a way to change that behaviour, though that might
be for the cost of speed.
As of L<HTML::Template::Compiled> 0.85 you can use this syntax:
<tmpl_if arrayref# >true<tmpl_else>false</tmpl_if >
In L<HTML::Template::Compiled::Classic> 0.04 it works as in HTML::Template.
=item debug_cache
Additional to 0 or 1 it can take an array ref for debugging only specific
cache operations.
=back
Note: the following is deprecated:
To be compatible in all of the above options all use:
use HTML::Template::Compiled compatible => 1;
If you don't care about these options you should use
use HTML::Template::Compiled speed => 1;
which is the default but depending on user wishes that might change.
=head2 DEPRECATED
=over 4
=item class methods ExpireTime, EnableSub, CaseSensitive, SearchPathOnInclude, UseQuery
=item option formatter_path
=item tag USE_VARS, not needed anymore
=item option cache_dir (replaced by file_cache_dir)
=item options method_call, deref, default_path, dumper
=item import tags short, compatible, speed
=back
=head2 ESCAPING
Like in HTML::Template, you have C<ESCAPE=HTML>, C<ESCAPE=URL> and C<ESCAPE_JS>.
C<ESCAPE=HTML> will only escape '"&<>. If you want to escape more, use
C<ESCAPE=HTML_ALL>.
Additionally you have C<ESCAPE=DUMP>, which by default will generate a Data::Dumper output.
You can also chain different escapings, like C<ESCAPE=DUMP|HTML>.
Additionally to ESCAPE=JS you have ESCAPE=IJSON which does not escape the
single quote.
=head2 INCLUDE
Additionally to
<TMPL_INCLUDE NAME="file.htc">
you can do an include of a template variable:
<TMPL_INCLUDE_VAR NAME="file_include_var">
$htc->param(file_include_var => "file.htc");
Using C<INCLUDE VAR="..."> is deprecated.
You can also include strings:
template:
inc: <%include_string foo %>
code:
$htc->param(
foo => 'included=<%= bar%>',
bar => 'real',
);
output:
inc: included=real
Note that included strings are not cached and cannot include files
or strings themselves.
=head2 EXTENDED VARIABLE ACCESS
With HTC, you have more control over how you access your template
parameters. An example:
my %hash = (
SELF => '/path/to/script.pl',
LANGUAGE => 'de',
BAND => 'Bauhaus',
ALBUMS => [
{
NAME => 'Mask',
SONGS => [ { NAME => 'Hair of the Dog' }, ... ],
},
],
INFO => {
BIOGRAPHY => '...',
LINK => '...'
},
NAME => "Cool script",
);
Now in the TMPL_LOOP C<ALBUMS> you would like to access the path to
your script, stored in $hash{SELF}. in HTML::Template you have to set
the option C<global_vars>, so you can access C<$hash{SELF}> from
everywhere. Unfortunately, now C<NAME> is also global, which might not
a problem in this simple example, but in a more complicated template
this is impossible. With HTC, you wouldn't use C<global_vars> here, but
you can say:
<TMPL_VAR .SELF>
to access the root element, and you could even say C<.INFO.BIOGRAPHY>
or C<ALBUMS[0].SONGS[0].NAME> (the latter has changed since version 0.79)
=head2 RENDERING OBJECTS
This is still in development, so I might change the API here.
Additionally to feeding a simple hash to HTC, you can feed it objects.
To do method calls you can also use '.' in the template.
my $htc = HTML::Template::Compiled->new(
...
);
$htc->param(
VAR => "blah",
OBJECT => bless({...}, "Your::Class"),
);
<TMPL_VAR NAME="OBJECT.fullname">
<TMPL_WITH OBJECT>
Name: <TMPL_VAR fullname>
</TMPL_WITH>
C<fullname> will call the fullname method of your Your::Class object.
It's recommended to just use the default . value for methods and dereferencing.
I might stop supporting that you can set the values for method calls by setting
an option. Ideally I would like to have that behaviour changed only by inheriting.
=head2 RUNNING PERL WITH TMPL_PERL
Yes, templating systems are for separating code and templates. But
as it turned out to be implemented much easier than expressions i
decided to implement it. But expressions are also available with the option
C<use_expressions>.
Note: If you have templates that can be edited by untrustworthy persons then
you don't want them to include perl code.
So, how do you use the perl-tag? First, you have to set the option
C<use_perl> to C<1> when creating a template object.
Important note: don't use C<print> in the included code. Usually the
template code is concatenated and returned to your perl script.
To 'print' something out use
__OUT__ 2**3;
This will be turned into something like
$OUT .= 2**3;
# or
print $fh 2**3;
Important note 2: HTC does not parse Perl. if you use the
classic tag-delimiters like this:
<tmpl_perl if (__CURRENT__->count > 42) { >
this will not work as it might seem. Use other delimiters
instead:
<%perl if (__CURRENT__->count > 42) { %>
Example:
<tmpl_loop list>
<tmpl_perl unless (__INDEX__ % 3) { >
</tr><tr>
<tmpl_perl } >
</tmpl_loop list>
# takes the current position of the parameter
# hash, key 'foo' and multiplies it with 3
<%perl __OUT__ __CURRENT__->{foo} * 3; %>
List of special keywords inside a perl-tag:
=over 4
=item __OUT__
Is turned into C<$OUT .=> or C<print $fh>
=item __HTC__
Is turned into the variable containing the current template object.
=item __CURRENT__
Turned into the variable containing the current position
in the parameter hash.
=item __ROOT__
Turned into the variable containing the parameter hash.
=item __INDEX__
Turned into the current index of a loop (starting with 0).
=back
=head2 INHERITANCE
It's possible since version 0.69 to inherit from HTML::Template::Compiled.
It's just not documented, and internal method names might change in
the near future. I'll try to fix the API and document which methods
you can inherit.
=head3 METHODS TO INHERIT
=over 4
=item method_call
Default is C<sub method_call { '.' }>
=item deref
Default is C<sub deref { '.' }>
=item formatter_path
Deprecated, see L<HTML::Template::Compiled::Formatter> please.
=item compile_early
Define if every included file should be checked and parsed at compile time
of the including template or later when it is really used.
Default is C<sub compile_early { 1 }>
=item parser_class
Default is C<sub parser_class { 'HTML::Template::Compiled::Parser' }>
You can write your own parser class (which must inherit from
L<HTML::Template::Compiled::Parser>) and use this.
L<HTML::Template::Compiled::Lazy> uses this.
=back
=head2 DEBUGGING
For printing out the contents of all the parameters you can do:
<TMPL_LOOP ALBUMS>
Dump: <TMPL_VAR _ ESCAPE=DUMP|HTML>
</TMPL_LOOP>
The special name C<_> gives you the current parameter and C<ESCAPE=DUMP>
will by default generate a Data::Dumper output of the
current variable, in this case it will dump out the contents of every
album in a loop. To correctly display that in html C<|HTML> will escape html
entities.
=head2 TMPL_WITH
If you have a deep leveled hash you might not want to always write
THE.FULL.PATH.TO.YOUR.VAR. Jump to your desired level once and
then you need only one level. Compare:
<TMPL_WITH DEEP.PATH.TO.HASH>
<TMPL_VAR NAME>: <TMPL_VAR AGE>
</TMPL_WITH>
<TMPL_VAR DEEP.PATH.TO.HASH.NAME>: <TMPL_VAR DEEP.PATH.TO.HASH.AGE>
Inside TMPL_WITH you can't reference parent nodes unless you're using global_vars.
=head2 TMPL_LOOP
The special name C<_> gives you the current parameter. In loops you can use it like this:
<tmpl_loop foo>
Current item: <tmpl_var _ >
</tmpl_loop>
Also you can give the current item an alias. See L<"ALIAS">.
The LOOP tag allows you to define a JOIN attribute:
<tmpl_loop favourite_colors join=", ">
<tmpl_var _ >
</tmpl_loop>
This will output something like C<blue, pink, yellow>.
This is easier than doing:
<tmpl_loop favourite_colors>
<tmpl_unless __first__>, </tmpl_unless>
<tmpl_var _ >
</tmpl_loop>
The C<LOOP>, C<WHILE> and C<EACH> tags allow you to define a BREAK attribute:
<tmpl_loop bingo break="3"> <tmpl_var _ ><if __break__>\n</if></tmpl_loop>
$htc->param(bingo => [qw(X 0 _ _ X 0 _ _ X)]);
outputs
X 0 _
_ X 0
_ _ X
So specifying BREAK=3 sets __break__ to 1 every 3rd loop iteration.
TMPL_LOOP expects an array reference, also if it is a method call. If
you want to iterate with TMPL_LOOP over a list from a method call, set
the attribute C<context=list>:
<tmpl_loop object.list_method context=list>
<tmpl_var _ >
</tmpl_loop>
=head2 TMPL_WHILE
Useful for iterating, for example over database resultsets.
The directive
<tmpl_while resultset.fetchrow>
<tmpl_var _.0>
</tmpl_while>
will work like:
while (my $row = $resultset->fetchrow) {
print $row->[0];
}
So the special variable name _ is set to the current item returned
by the iterator.
You also can use L<"ALIAS"> here.
=head2 TMPL_EACH
Iterating over a hash. Internally it is not implemented as an each, so you
can also sort the output:
Sorted alphanumerically by default (since 0.93):
<tmpl_each letters >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each letters >
Sorted numerically:
<tmpl_each numbers sort=num >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each numbers >
Not sorted:
<tmpl_each numbers sort=0 >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each numbers >
Sorted alphanumerically:
<tmpl_each letters sort=alpha >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each letters >
You have to set the option C<loop_context_vars> to true to use
the special vars C<__key__> and C<__value__>.
If you want to iterate over a hash instead of a hashref (some
methods might return plain hashes instead of references and
TMPL_EACH expects a ref), then you can set C<context=list>:
<tmpl_each object.hash_method context=list>
<tmpl_var __key__ >
</tmpl_each>
Since 1.000_001 you can also define by which variable you want to sort.
If you have a hash with hashes as values:
$htc->param(
letters => {
1 => { letter =>'b' },
2 => { letter =>'a' },
3 => { letter =>'c' },
},
);
<%each letters sort=alpha sortby="letter" %>
<%set_var val value=__value__ %>
<%= __key__ %> = <%= $val.letter %>
<%/each%>
=head2 SET_VAR
Since 0.96_002
Sets a local variable to the value given in C<value> or C<expr>
<tmpl_set foo expr=23>
<tmpl_set name=bar expr=23>
<tmpl_set boo value=var.boo>
<tmpl_set oof expr="21*2">
<tmpl_var $foo>
<tmpl_var $bar>
...
C<value=..> behaves like a variable name from the parameter stash.
The variable name to set must match /[0-9a-z_]+/i
You can refer to an alias via C<$alias> or simply C<alias>. Note that
the latter syntax is not recommended any more since it can conflict
with parameters from the stash.
If you want to use aliases in includes, you need to use the C<$alias>
syntax.
=head2 USE_VARS
deprecated. Was added in 0.96_004 to make it possible to use aliases
set with C<alias=...> or C<SET_VAR> in includes. Now you should rather
use the <$alias> syntax.
The following explanation is just there for history and will be removed
some time in the future. For now it still works.
Necessary if you want vars like SET_VAR and loop aliases from outside
in includes.
Before the first use in the include, add:
<tmpl_use_vars foo,bar,boo >
so that the compiler recognizes them as user defined vars and not parameters
from the stash.
This statement is valid until the end of the template so you cannot
"overwrite" parameters of the stash locally.
=head2 WRAPPER
Since 0.97_005. Experimental. Please test.
Needs option C<loop_context_vars>.
Works similar to WRAPPER in Template-Toolkit.
Is similar to TMPL_INCLUDE, just that the included wrapper is wrapped
around the content. It can be used to avoid including head and foot separately.
<tmpl_wrapper wrapper.html >
content: some var: <tmpl_var foo >
</tmpl_wrapper>
In wrapper.html the special loop context var C<__wrapper__> is used for
the included content:
wrapper.html:
<some><layout>
<tmpl_var __wrapped__ >
</layout></some>
Important notes:
If you are using C<out_fh> to print directly to a filehandle instead of
returning to a string, this feature might not be useful, since it is
appending the content inside of the wrapper to a string and prints it
when it comes to the end of the wrapper tag.
So if you are using C<out_fh> to avoid generating long strings in
memory, you should rather use TMPL_INCLUDE instead.
Also you need perl 5.8 or higher to use it in combination with out_fh.
=head2 TMPL_COMMENT
For debugging purposes you can temporarily comment out regions:
Wanted: <tmpl_var wanted>
<tmpl_comment outer>
this won't be printed
<tmpl_comment inner>
<tmpl_var unwanted>
</tmpl_comment inner>
<tmpl_var unwanted>
</tmpl_comment outer>
$htc->param(unwanted => "no thanks", wanted => "we want this");
The output is (whitespaces stripped):
Wanted: we want this
HTC will ignore anything between COMMENT directives.
This is useful for debugging, and also for documentation inside the
template which should not be outputted.
=head2 TMPL_NOPARSE
Anything between
<tmpl_noparse>...</tmpl_noparse>
will not be recognized as template directives. Same syntax as TMPL_COMMENT.
It will output the content, though.
=head2 TMPL_VERBATIM
Anything between
<tmpl_verbatim>...</tmpl_verbatim>
will not be recognized as template directives. Same syntax as
L<"TMPL_NOPARSE">, but it will be HTML-Escaped. This can be
useful for debugging.
=head2 TMPL_SWITCH
The SWITCH directive has the same syntax as VAR, IF etc.
The CASE directive takes a simple string or a comma separated list of strings.
Yes, without quotes. This will probably change! I just don't know yet
how it should look like. Suggestions?
With that directive you can do simple string comparisons.
<tmpl_switch language>(or <tmpl_switch name=language>)
<tmpl_case de>echt cool
<tmpl_case en>very cool
<tmpl_case es>superculo
<tmpl_case fr,se>don't speak french or swedish
<tmpl_case default>sorry, no translation for cool in language <%=language%> available
<tmpl_case>(same as default)
</tmpl_switch>
It's also possible to specify the default with a list of other strings:
<tmpl_case fr,default>
Note that the default case should always be the last statement before the
closing switch.
=head2 OPTIONS
As you can cache the generated perl code in files, some of the options are fixed; that means
for example if you set the option case_sensitive to 0 and the next time you call the same template
with case_sensitive 1 then this will be ignored. The options below will be marked as (fixed).
=over 4
=item path
Path to template files
=item search_path_on_include
Search the list of paths specified with C<path> when including a template.
Default is 0
See L<"DIFFERENT FEATURES"> for the additional possible value 2.
=item file_cache
Set to 1 if you want to use file caching and specify the path
with file_cache_dir.
=item file_cache_dir
Path to caching directory (you have to create it before)
=item cache_dir
Replaced by file_cache_dir like in L<HTML::Template>. Will be deprecated
in future versions.
=item cache
Is 1 by default. If set to 0, no memory caching is done. Only recommendable if
you have a dynamic template content (with scalarref, arrayre for example).
=item expire_time
Recheck template files on disk after C<expire_time> seconds. See L<"CACHING">
=item filename
Template to parse
=item scalarref
Reference to a scalar with your template content. It's possible to cache
scalarrefs, too, if you have Digest::MD5 installed. Note that your cache directory
might get filled with files from earlier versions. Clean the cache regularly.
Don't cache scalarrefs if you have dynamic strings. Your memory might get filled up fast!
Use the option
cache => 0
to disable memory caching.
=item arrayref
Reference to array containing lines of the template content (newlines have
to be included)
=item filehandle
Filehandle which contains the template content. Note that HTC will not cache
templates created like this.
=item loop_context_vars (fixed)
Vars like C<__first__>, C<__last__>, C<__inner__>, C<__odd__>, C<__counter__>,
C<__index__>, C<__outer__>, C<__even__>
The variable C<__index__> works just like C<__counter__>, only that it starts
at 0 instead of 1.
=item global_vars (fixed)
If set to 1, every outer variable can be accessed from anywhere in the enclosing scope.
Default is 0.
Note that I don't recommend using global_vars. For referring to parameters
up in the stash you can use aliases via C<alias=...> or C<SET_VAR>.
See L<"ALIAS"> and L<"SET_VAR">.
If you still would like to be able to navigate up the parameter stash, you
have the following option:
If set to 2, you don't have global vars, but have the possibility to go
up the stack one level. Example:
<tmpl_var ...key>
This will get you up 2 levels (remember: one dot means root in HTC) and access the 'key'
element.
If set to 3 (C<3 == 1|2>) you have both, global vars and explicitly going up the stack.
So setting global_vars to 2 can save you from global vars but still allows you to
browse through the stack.
=item default_escape
my $htc = HTML::Template::Compiled->new(
...
default_escape => 'HTML', # or URL
);
Now everything will be escaped for HTML unless you explicitly specify C<ESCAPE=0> (no escaping)
or C<ESCAPE=URL>.
=item strict (since 0.97_001)
Default: 1
If set to 0 unknown tags will be ignored and output verbatim:
<TMPL_FOOBAR anything ... <TMPL_VAR valid>
=item line_info (fixed) (since 1.000_004)
Default: 0
my $htc = HTML::Template::Compiled->new(
...
line_info => 1, # default 0
);
If any runtime errors occur, line information will output the template
filename and line (instead of "eval" and the generated perl code line)
=item warnings (fixed) (since 1.000_004)
Default: 0
If set to 1, runtime warnings (like use of uninitialized value) will be
output to stderr.
If set to 'fatal', any runtime warning will cause the script to die.
=item no_includes (since 0.92)
Default is 0. If set to 1, the tags INCLUDE, INCLUDE_VAR and INCLUDE_STRING
will cause a template syntax error when creating. This can be useful when opening
untrusted templates, otherwise any file in the filesystem could be opened.
=item debug_file (fixed) (since 0.91_001)
Additionally to the context_vars __filename__ and __filenameshort__ you
can enable filename debugging globally.
If the option is set to 'start', at the start of every template will be added:
<!-- start templates/path/filename.html -->
If set to 'end', at the end will be added:
<!-- end templates/path/filename.html -->
If set to 'start,end', both comments will be added.
If set to 'start,short', 'end,short' or 'start,end,short' the path
to the templates will be stripped:
<!-- start path/filename.html -->
<!-- end path/filename.html -->
=item optimize (fixed) (since 1.001_001)
Hashref with compiler hints.
Every access to the parameter stash has to check if the current var is
an object or a hash. This allows you to use the same notation for hash accesses
and method calls without caring about the data.
But this is quite expensive. You can give the compiler hints:
HTML::Template::Compiled->new(
optimize => {
initial_var => 1, # defaults
object_check => 0,
root_hash => 0,
},
=over 4
=item initial_var
Default: 1
Might become a default in the code itself and removed as an option. Report if
you have problems and set it to 0.
This is just a minor internal optimization for variable accesses like
C<[%= foo.bar.baz %]>
=item object_check
Default: 0
If you are in a loop and make several accesses to the same var, it always
checks if it is an object or not:
[%loop threads %]
[%= id %]
[%= title %]
[%= ctime %]
...
[%/loop threads %]
If you set this to true, the check will be done at the beginning of the loop
and saved into a variable, so that subsequent accesses only use the check
variable.
Same for TMPL_WITH, TMPL_WHILE.
If you only have one access in a loop, this might be unnecessary overhead.
Also, theoretically, a variable can change during calls.
In the most cases this option should be fine. I will set the default to 1
someday probably.
=item root_hash
It is possible to pass an object to param() instead of a hash.
So even every access to the root of the parameter stash has to check if
it is an object or a hashref.
In the most cases the parameter stash is a hashref. If you are sure that you
always have a parameter hash and activate this option, the compiler can
avoid this check.
=back
=item objects (fixed) (since 0.91_001)
if set to true, you can use method calls like
<%= object.method %>
Default is 'strict' (true).
If set to 'strict', the method will be called if we have an object, otherwise
it's treated as a hash lookup. If the method doesn't exist, it dies.
If set to 'nostrict', the method will be called only if the object 'can' do the
method, otherwise it will return undef (this will need Scalar::Util).
If set to 0, no method calls are allowed.
=item deref (fixed)
Deprecated. Please inherit and overwrite method 'deref'. See L<"INHERITANCE">
Define the string you want to use for dereferencing, default is C<.> at the
moment:
<TMPL_VAR hash.key>
=item method_call (fixed)
Deprecated. Please inherit and overwrite method 'method_call'. See L<"INHERITANCE">
Define the string you want to use for method calls, default is . at
the moment:
<TMPL_VAR object.method>
Don't use ->, though, like you could in earlier version. Var names can contain:
Numbers, letters, '.', '/', '+', '-' and '_', just like HTML::Template. Note that
if your var names contain dots, though, they will be treated as hash
dereferences. If you want literal dots, use L<HTML::Template::Compiled::Classic>
instead.
=item default_path (fixed)
Deprecated, see L<HTML::Template::Compiled::Formatter> please.
my $htc = HTML::Template::Compiled->new(
...
default_path
# default is PATH_DEREF
=> HTML::Template::Compiled::Utils::PATH_FORMATTER,
);
Is needed if you have an unqualified tmpl_var that should be resolved as
a call to your formatter, for example. Otherwise you have to call it
fully qualified. If your formatter_path is '/', you'd say tmpl_var C<_/method>.
With the option default_path you can make that the default, so you don't need
the C<_/>: C<tmpl_var method>. If you don't use formatters, don't care about
this option.
=item line_numbers
NOTE: This option does not exist any more; line numbers will always be reported.
For debugging: prints the line number of the wrong tag, e.g. if you have
a /TMPL_IF that does not have an opening tag.
=item case_sensitive (fixed)
default is 1, set it to 0 to use this feature like in HTML::Template. Note that
this can slow down your program a lot (50%).
=item dumper
This option is deprecated as of version 0.76. You must now use a plugin instead, like
L<HTML::Template::Compiled::Plugin::DHTML>, for example.
my $t = HTML::Template::Compiled->new(
...
dumper = sub { my_cool_dumper($_[0]) },
);
---
<TMPL_VAR var ESCAPE=DUMP>
This will call C<my_cool_dumper()> on C<var>.
Alternatively you can use the DHTML plugin which is using C<Data::TreeDumper> and
C<Data::TreeDumper::Renderer::DHTML>. You'll get a dumper like output which you can
collapse and expand, for example. See L<Data::TreeDumper> and L<Data::TreeDumper::Renderer::DHTML> for
more information.
Example:
my $t = HTML::Template::Compiled->new(
...
dumper = 'DHTML',
);
For an example see C<examples/dhtml.html>.
=item out_fh (fixed)
my $t = HTML::Template::Compiled->new(
...
out_fh => 1,
);
...
$t->output($fh); # or output(\*STDOUT) or even output()
This option is fixed, so if you create a template with C<out_fh>, every
output of this template will print to a specified (or default C<STDOUT>) filehandle.
=item filter
Filter template code before parsing.
my $t = HTML::Template::Compiled->new(
...
filter => sub { myfilter( ${$_[0]} ) },
# or
filter => [ {
sub => sub { myfilter( ${$_[0]} ) },
format => 'scalar', # or array
},
...
],
);
=item tagstyle (fixed)
Specify which styles you want to use. This option takes an arrayref
with strings of named tagstyles or your own regexes.
At the moment there are the following named tagstyles builtin:
# classic (active by default)
<TMPL_IF foo><tmpl_var bar></TMPL_IF>
# comment (active by default)
<!-- TMPL_IF foo --><!-- TMPL_VAR bar --><!-- /TMPL_IF -->
# asp (active by default)
<%if foo%><%VAR bar%><%/if%>
# php (not active by default)
<?if foo?><?var bar?><?/if foo?>
# tt (not active by default)
[%if foo%][%var bar%][%/if foo%]
You deactivate a style by saying -stylename. You activate by saying
+stylename.
Define your own tagstyle by specifying regexes. For example
you want to use {C<{{if foo}}{{var bar}}{{/if foo}}>, then your
definition should be:
[
qr({{), # start of opening tag
qr(}}), # end of opening tag
qr({{/), # start of closing tag
qr(}}), # end of closing tag
]
NOTE: do not specify capturing parentheses in you regexes. If you
need parentheses, use C<(?:foo|bar)> instead of C<(foo|bar)>.
Say you want to deactivate asp-style, comment-style, activate php- and
tt-style and your own C<{{}} > style, then say:
my $htc = HTML::Template::Compiled->new(
...
tagstyle => [
qw(-asp -comment +php +tt),
[ qr({{), qr(}}), qr({{/), qr(}})],
],
);
=item use_expressions (since 0.91_003)
Set to 1 if you want to use expressions. The basic expressions work more or
less like in L<HTML::Template::Expr> - I took the parsing code from it and
used it with some minor changes - thanks to Sam Tregar.
<%if expr="some.var > 3" %>It's grater than 3<%/if %>
But with expressions you can also use more complex navigation through the
template stash:
You can use object methods with parameters. While a
normal method call can only be called without parameters, like
<%= object.name %>
with expressions you can give it parameters:
<%= expr="object.create_link('navi')" %>
Inside function and method calls, hash keys you also can use template
vars (array indices and hash keys since 0.96_003).
<%= expr=".path.to.hash{var}" %>
<%= expr=".path.to.hash{.another.var[123]}{'literal key'}" %>
It is only minimally tested yet, so use with care and please report any
bugs you find.
A useful example: Output a number of items with their prices formatted.
my $nf = Number::Format->new(...);
my $htc = HTML::Template::Compiled->new(
filename => 'items.html',
use_expressions => 1,
);
$htc->param(
items => [
{ size => 50 * 1024 * 1024 * 1024, price => 49.95 },
{ size => 250 * 1024 * 1024 * 1024, price => 110.99 },
],
nf => $nf,
);
items.html:
<%loop .items %>
Size: <%= expr=".nf.format_bytes(size)" %>
Price: <%= expr=".nf.format_price(price)" %>
<%/loop %>
Output:
Size: 50G
Price: 49,95 EUR
Size: 250G
Price: 110,99 EUR
=item formatter
Deprecated, see L<HTML::Template::Compiled::Formatter> please.
With formatter you can specify how an object should be rendered. This is useful
if you don't want object methods to be called, but only a given subset of
methods.
my $htc = HTML::Template::Compiled->new(
...
formatter => {
'Your::Class' => {
fullname => sub {
$_[0]->first . ' ' . $_[0]->last
},
first => Your::Class->can('first'),
last => Your::Class->can('last'),
},
},
);
# $obj is a Your::Class object
$htc->param(obj => $obj);
# Template:
# Fullname: <tmpl_var obj/fullname>
=item formatter_path (fixed)
Deprecated, see L<HTML::Template::Compiled::Formatter> please.
=item debug
If set to 1 you will get the generated perl code on standard error
=item use_query
Set it to 1 if you plan to use the query() method. Default is 0.
Explanation: If you want to use query() to collect information
on the template HTC has to do extra-work while compiling and
uses extra-memory, so you can choose to save HTC work by
setting use_query to 0 (default) or letting HTC do the extra
work by setting it to 1. If you would like 1 to be the default,
write me. If enough people write me, I'll think about it =)
=item use_perl
Set to 1 if you want to use the perl-tag. See L<"TMPL_PERL">. Default is 0.
=item cache_debug
Default: 0
You can debug hits and misses for file cache and memory cache:
# debug all cache
my $htc = HTML::Template::Compiled->new(
cache_debug => 1,
...
);
# only debug misses
my $htc = HTML::Template::Compiled->new(
cache_debug => [qw/ file_miss mem_miss /],
...
);
Possible values when passing an array ref: file_miss file_hit mem_miss mem_hit
Output looks similar to HTML::Template cache_debug and will be output
to STDERR via warn().
=back
=head2 METHODS
=over 4
=item clear_cache ([DIR])
Class method. It will clear the memory cache either of a specified cache directory:
HTML::Template::Compiled->clear_cache($cache_dir);
or all memory caches:
HTML::Template::Compiled->clear_cache();
=item clear_filecache
Class- or object-method. Removes all generated perl files from a given directory.
# clear a directory
HTML::Template::Compiled->clear_filecache('cache_directory');
# clear this template's cache directory (and not one template file only!)
$htc->clear_filecache();
=item param
Works like in L<HTML::Template>.
=item query
Works like in L<HTML::Template>. But it is not activated by default. If you want
to use it, specify the use_query option.
=item preload
Class method. Will preload all template files from a given cachedir into memory. Should
be done, for example in a mod_perl environment, at server startup, so all templates
go into "shared memory"
HTML::Template::Compiled->preload($cache_dir);
If you don't do preloading in mod_perl, memory usage might go up if you have a lot
of templates.
Note: the directory is *not* the template directory. It should be the directory
which you give as the file_cache_dir option.
=item precompile
Class method. It will precompile a list of template files into the specified
cache directory. See L<"PRECOMPILE">.
=item clear_params
Empty all parameters.
=item debug_code (since 0.91_003)
If you get an error from the generated template, you might want to debug
the executed code. You can now call C<debug_code> to get the compiled code
and the line the error occurred. Note that the reported line might not be
the exact line where the error occurred, also look around the line.
The template filename reported does currently only report the main template,
not the name of an included template. I'll try to fix that.
local $HTML::Template::Compiled::DEBUG = 1;
my $htc = HTML::Template::Compiled->new(
filename => 'some_file_with_runtime_error.html',
);
eval {
print $htc->output;
};
if ($@) {
# reports as text
my $msg = $htc->debug_code;
# reports as a html table
my $msg_html = $htc->debug_code('html');
}
=item get_plugin
my $plugin = $htc->get_plugin('Name::of::plugin');
Returns the plugin object of that classname. If the plugin is only a string
(the classname itself), it returns this string, so this method is only
useful for plugin objects.
=item var2expression
Useful for plugins. Parses a template var (C<name="foo.bar.baz"> and returns
the perl expression for the compiler.
=back
=head1 EXPORT
None.
=head1 CACHING
You create a template almost like in HTML::Template:
my $t = HTML::Template::Compiled->new(
path => 'templates',
loop_context_vars => 1,
filename => 'test.html',
# for testing without cache comment out
file_cache => 1,
file_cache_dir => "cache",
);
The next time you start your application and create a new template, HTC will read all generated
perl files, and a call to the constructor like above won't parse
the template, but just use the loaded code. If your template
file has changed, though, then it will be parsed again.
You can set the expire time of a template by passing the option
expire_time => $seconds
Note that
HTML::Template::Compiled->ExpireTime($seconds);
C<$HTML::Template::Compiled::NEW_CHECK>
are deprecated since they change a global variable which is then
visible in the whole process, so in persistent environments other apps
might be affected.
So an expire time of 600 seconds (default)
will check after 10 minutes if the tmpl file was modified. Set it to a
very high value will then ignore any changes, until you delete the
generated code.
For development you should set it to 0, for a pre-production server
you can set it to 60 seconds, for example. It can make quite a difference.
=head1 PLUGINS
At the moment you can use and write plugins for the C<ESCAPE> attribute. See
L<HTML::Template::Compiled::Plugin::XMLEscape> for an example how to
use it; and have a look at the source code if you want to know how to
write a plugin yourself.
Using Plugins:
my $htc = HTML::Template::Compiled->new(
...
plugin => ['HTML::Template::Compiled::Foo::Bar'],
# oor shorter:
plugin => ['::Foo::Bar'],
);
=head1 LAZY LOADING
Let's say you're in a CGI environment and have a lot of includes in your
template, but only few of them are actually used. HTML::Template::Compiled
will (as L<HTML::Template> does) parse all of your includes at once.
Just like the C<use> function does in perl. To get a behaviour like
require, use L<HTML::Template::Compiled::Lazy>.
=head1 TODO
associate, methods with simple parameters,
expressions, pluggable, ...
=head1 IMPLEMENTATION
HTC generates a perl subroutine out of every template. Each included template
is a subroutine for itself. You can look at the generated code by activating
file caching and looking into the cache directory. When you call C<output()>,
the subroutine is called. The subroutine either creates a string and adds
each template text or the results of the tags to the string, or it prints
it directly to a filehandle. Because of the implementation you have to know
at creation time of the module if you want to get a string back or if you
want to print to a filehandle.
=head1 SECURITY
HTML::Template::Compiled uses basically the same file caching model as, for example, Template-
Toolkit does: The compiled Perl code is written to disk and later reread via C<do> or
by reading the file and C<eval> the content.
If you are sharing a read/write environment with untrusted users (for example on
a machine with a webserver, like many webhosters offer, and all scripts are running
as the same httpd user), realize that there is possibility of modifying the Perl code that is
cached and then executed. The best solution is to not be in such an
environment!
In this case it is the safest option to generate your compiled templates on a local machine
and just put the compiled templates onto the server, with no write access for the http server.
Set the C<expire_time> option to a high value so that HTC never attempts to check the
template timestamp to force a regenerating of the code.
If you are alone on the machine, but you are running under taint mode (see L<perlsec>) then
you have to explicitly set the C<$UNTAINT> variable to 1. HTC will then untaint the code for you
and treat it as if it were safe (it hopefully is =).
=head1 PRECOMPILE
I think there is no way to provide an easy function for precompiling,
because every template can have different options.
If you have all your templates with the same options, then you can use the
precompile class method.
It works like this:
HTML::Template::Compiled->precompile(
# usual options like path, default_escape, global_vars, file_cache_dir, ...
filenames => [ list of template-filenames ],
);
This will then pre-compile all templates into file_cache_dir. Now you would just put this
directory onto the server, and it doesn't need any write-permissions, as it
will be never changed (until you update it because templates have changed).
=head1 BENCHMARKS
The options C<case_sensitive>, C<loop_context_vars> and C<global_vars> can have the biggest influence
on speed.
Setting case_sensitive to 1, loop_context_vars to 0 and global_vars to 0 saves time.
On the other hand, compared to HTML::Template, you have a large speed gain under mod_perl
if you use case_sensitive = 1, loop_context_vars = 0,
With CGI HTC is slower.
See the C<examples/bench.pl> contained in this distribution.
Here are some examples from the benchmark script. I'm showing only Template::AutoFilter,
Template::HTML, HTML::Template and HTC. These four modules allow you to set
automatic HTML escaping ('filter') for all variables.
loop_context_vars 1
global_vars 0
case_sensitive 1
default_escape HTML (respectively Template::AutoFilter and Template::HTML)
ht: HTML::Template 2.10
htc: HTML::Template::Compiled 0.95
ttaf: Template::AutoFilter 0.112350 with Template 2.22
tth: Template::HTML 0.02 with Template 2.22
First test is with the test.(htc|tt) from the examples directory, about 900 bytes.
Test without file cache and without memory cache.
all_ht: 1 wallclock secs ( 0.40 usr + 0.00 sys = 0.40 CPU) @ 250.00/s (n=100)
all_htc: 1 wallclock secs ( 1.74 usr + 0.01 sys = 1.75 CPU) @ 57.14/s (n=100)
all_ttaf_new_object: 1 wallclock secs ( 1.69 usr + 0.01 sys = 1.70 CPU) @ 58.82/s (n=100)
all_tth_new_object: 1 wallclock secs ( 1.44 usr + 0.00 sys = 1.44 CPU) @ 69.44/s (n=100)
With file cache:
all_ht: 1 wallclock secs ( 1.03 usr + 0.01 sys = 1.04 CPU) @ 379.81/s (n=395)
all_htc: 1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 260.75/s (n=279)
all_ttaf_new_object: 1 wallclock secs ( 1.07 usr + 0.04 sys = 1.11 CPU) @ 251.35/s (n=279)
all_tth_new_object: 1 wallclock secs ( 1.01 usr + 0.04 sys = 1.05 CPU) @ 227.62/s (n=239)
With memory cache:
all_ht: 1 wallclock secs ( 1.04 usr + 0.00 sys = 1.04 CPU) @ 461.54/s (n=480)
all_htc: 1 wallclock secs ( 1.05 usr + 0.01 sys = 1.06 CPU) @ 3168.87/s (n=3359)
process_ttaf: 1 wallclock secs ( 1.04 usr + 0.00 sys = 1.04 CPU) @ 679.81/s (n=707)
process_tth: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 609.52/s (n=640)
Now I'm using a template with about 18Kb by multiplying the example template
20 times. You can see that everything is running slower but some run more
slower than others.
Test without file cache and without memory cache.
all_ht: 8 wallclock secs ( 7.57 usr + 0.04 sys = 7.61 CPU) @ 13.14/s (n=100)
all_htc: 32 wallclock secs (32.08 usr + 0.06 sys = 32.14 CPU) @ 3.11/s (n=100)
all_ttaf_new_object: 36 wallclock secs (36.21 usr + 0.04 sys = 36.25 CPU) @ 2.76/s (n=100)
all_tth_new_object: 29 wallclock secs (28.92 usr + 0.05 sys = 28.97 CPU) @ 3.45/s (n=100)
With file cache:
all_ht: 8 wallclock secs ( 7.22 usr + 0.00 sys = 7.22 CPU) @ 13.85/s (n=100)
all_htc: 5 wallclock secs ( 5.32 usr + 0.00 sys = 5.32 CPU) @ 18.80/s (n=100)
all_ttaf_new_object: 8 wallclock secs ( 7.59 usr + 0.15 sys = 7.74 CPU) @ 12.92/s (n=100)
all_tth_new_object: 9 wallclock secs ( 8.74 usr + 0.19 sys = 8.93 CPU) @ 11.20/s (n=100)
With memory cache:
all_ht: 1 wallclock secs ( 1.04 usr + 0.01 sys = 1.05 CPU) @ 15.24/s (n=16)
all_htc: 1 wallclock secs ( 1.12 usr + 0.00 sys = 1.12 CPU) @ 272.32/s (n=305)
process_ttaf: 1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 39.25/s (n=42)
process_tth: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 34.29/s (n=36)
So the performance difference highly depends on the size of the template and on the
various options.
You can see that using the 900byte template HTC is slower with file cache than
HTML::Template, but with the 18Kb template it's faster.
=head1 EXAMPLES
See L<examples/objects.html> (and C<examples/objects.pl>) for an example
how to feed objects to HTC.
=head1 BUGS
Probably many bugs I don't know yet =)
Use the bugtracking system to report a bug:
http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Template-Compiled
=head1 Why another Template System?
You might ask why I implement yet another templating system. There
are so many to choose from. Well, there are several reasons.
I like the syntax of HTML::Template *because* it is very restricted.
It's also easy to use (template syntax and API).
However, there are some things I miss I try to implement here.
I think while HTML::Template is quite good, the implementation can
be made more efficient (and still pure Perl). That's what I'm trying to achieve.
I use it in my web applications, so I first write it for myself =)
If I can efficiently use it, it was worth it.
=head1 RESOURCES
See http://htcompiled.sf.net/ for svn access.
=head1 SEE ALSO
L<HTML::Template>
L<HTML::Template::JIT>
L<Template> - Toolkit
http://www.tinita.de/projects/perl/
=head1 AUTHOR
Tina Mueller
=head1 CREDITS
Sam Tregar big thanks for ideas and letting me use his L<HTML::Template> test suite
Bjoern Kriews for original idea and contributions
Special Thanks to Sascha Kiefer - he finds all the bugs!
Ronnie Neumann, Martin Fabiani, Kai Sengpiel, Jan Willamowius, Justin Day,
Steffen Winkler, Henrik Tougaard for ideas, beta-testing and patches
L<http://www.perlmonks.org/> and http://www.perl-community.de/> for everyday learning
Corion, Limbic~Region, tye, runrig and others from perlmonks.org
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005-2012 by Tina Mueller
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.
=cut
|